Wordpress navigation using wp_dropdown_pages without submit button - wordpress

I'm tyring to make a simple drop down navigation GUI using wp_dropdown_pages() something along the lines of this non functioning code;
<form action="<?php bloginfo('url'); ?>" method="get">
<?php wp_dropdown_pages(array('echo' => '1')); ?>
<onchange='return this.form.submit()'>
</form>
I know its somethinng to do with the echo, I think,. PS Dont want to use a submit button. Many thanks in advance

Have worked out an what was going on from a wordpress [post]:
http://wordpress.org/support/topic/is-the-new-dropdown_categories-working?replies=13#post-657759
<?php wp_dropdown_categories('show_option_none=Select category'); ?>
<script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange()
{
if ( dropdown.options[dropdown.selectedIndex].value > 0 )
{
location.href = "<?php echo get_option('home');
?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
--></script>

Related

Set Wordpress post status to 'Draft' from front end similar to get_delete_post_link

I am using the below code to allow a logged in user to set delete their own posts from the front end. Is there a way to do the same thing but setting the post to 'draft' rather than deleting it completely?
<?php if ($post->post_author == $current_user->ID) { ?>
<p><a onclick="return confirm('Are you SURE you want to delete this?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>
<?php } ?>
use this function wp_update_post(), you can test with this example:
First create a form were you want to users select if the post are going to be a draft
<form action="" method="POST" >
<input type="checkbox" value="ok" name="draft">
<input type="submit" value="Ok">
</form>
Then create a function to save the new state put this in function.php:
function toDraft($pid){
$toDraft = $_POST['draft'];
if($toDraft == 'ok'){
echo "string";
wp_update_post(array('ID' => $pid, 'post_status' => 'draft'));
}
}
Then add this function below the form you create.
toDraft($post->ID);
And test. Read this to know more about update post status

Open ad clicking anywhere in wordpress

I have wordpress site and I'm using this script
onclick="<?php if(!isset($_COOKIE['visited'])){echo"window.open('http://link', '_blank')"; setcookie("visited", "1", time()+3600*24); header("Refresh:0");} ?>"
in body line. So it means I'm using
<body <?php body_class(); ?> onclick="<?php if(!isset($_COOKIE['visited'])){echo"window.open('http://link', '_blank')"; setcookie("visited", "1", time()+3600*24); header("Refresh:0");} ?>">
on header.php file of the theme.
But it opens the link in every click.
I want to open it only one time until I refresh the page or open new page.
How can I do it.
Please help.
Thanks in advance.
Greetings.
Like the setcookie function reference says:
cookies must be sent before any output from your script
First solution
You could add this at the very top oh header.php:
<?php
$onclick = '';
if(!isset($_COOKIE['visited'])) {
setcookie('visited', '1', time()+3600*24, COOKIEPATH, COOKIE_DOMAIN);
$onclick = 'onclick="window.open(\'http://link\', \'_blank\')"';
}
?>
Update: I've added the constants COOKIEPATH and COOKIE_DOMAIN like explained here.
And this for your body tag:
<body <?php body_class(); ?> <?php echo $onclick; ?>>
Better solution
The prior solution doesn't ensure the cookie is set before any output sent (there could be some characters in one of you template files before get_header() is called, or a plugin could add something).
Instead you should add an action to a Wordpress hook that is always called before any output (like init).
So try adding to your functions.php file:
add_action('init', 'set_visited_cookie');
function set_visited_cookie() {
if (!isset($_COOKIE['visited'])) {
setcookie('visited', '1', strtotime('+1 day'), COOKIEPATH, COOKIE_DOMAIN);
}
}
As for your body tag:
<body <?php body_class(); ?> <?php if(!isset($_COOKIE['visited'])): ?> onclick="window.open('http://link', '_blank')" <?php endif; ?>>

Jquery Mobile with 2 set of page

I have a wordpress site, that need to show pages using swipe, I choose to use Jquery Mobile, and I get it working fine. Now, we have 2 languages on site, using wpml plugin. And my Swipe Code works well, except when we use Change language button swipe fails.
Details on issue.
We have only 3 Text Only page in our website, in 2 language. And in Footer we have link to change language. Also client hate to have Ajax page loading, so what I did is I create three Div with data-role=page and put data-next, data-prev as #div-$postid. So the navigation works absolute fine. I put footer outside from data-role=page.
Now, when I click change button in footer, it load the english page [I saw it using Fiddler] and then take first data-role=page from server and replace /slide its content. However since it only pick the first data role, all other english page doesn't get in HTML [it just update DOM and doesn't navigate to english version]. so swipe fails as other english pages are not in dom.
Also, footer is not changing, so what I want is: can we simple force a Link to navigate instead of going swipe way? Jquery Mobile is enforcing swipe on all A tags, I do not want swipe to works anything outside data-role=page.
Hope I make sense.
Edit here is code: [not sure if this code will help at all]
<?php
get_header();
global $post;
$args = array('post_type' => 'mobile_slide','showposts' => '-1', "order" => "DESC");
$the_query = new WP_Query($args);
if($the_query->have_posts()){
while($the_query->have_posts()) { $the_query->the_post();
$prev =get_previous_post();
$next =get_next_post();
if($prev) {
$prev = "#page-" . $prev->ID; //get_permalink($prev->ID);
} else {
$prev='';
}
if($next) {
$next = "#page-".$next->ID; //get_permalink($next->ID);
} else {
$next='';
}
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'slider_image' ); ?>
<div id="page-<?php echo $post->ID; ?>" data-dom-cache="true" data-transition="slide" class="page" data-role="page" data-prev="<?php echo $prev; ?>" data-next="<?php echo $next; ?>" style="background-image:url('<?php echo $image[0]; ?>'); background-position:center center; background-color:#000; background-repeat:no-repeat; ">
<?php } else { ?>
<div id="page-<?php echo $post->ID; ?>" data-dom-cache="true" data-transition="slide" class="page" data-role="page" data-prev="<?php echo $prev; ?>" data-next="<?php echo $next; ?>">
<?php } ?>
<div class="post_box">
<h2><blockquote><?php the_title(); ?></blockquote></h2>
<div class="post_entry">
<?php the_content(); ?>
</div>
</div><!-- post_box -->
</div>
<?php }
} ?>
<?php get_footer(); ?>
This is all I have, except that get_footer use Ul li based list where on LI change based on language variable, to show different images for either language.
To stop Ajax from loading pages/links, add to link anchor data-rel="external" or data-ajax="false". This will load page normally without any transition.
Reference: jQuery Mobile - Links
For those who have similar problem, I fix it by using following:
1) I add a "noswipe" class to A Tag so I can refer it in Jquery
2) I add following code
$(function(){
$(".noswipe").click(function(){
window.location.href= $(this).attr("href");
return false;
});
});
The above code simply enforce to skip the Mobile's parsing and calling and works for my case.

Add meta box to WordPress options page

How can I add a (draggable) meta box to an options page for a plugin I created? Is this even possible? Because if I look in the docs I see that I can only add it to a 'post', 'page', 'link', or 'custom_post_type'.
Yes it's possible. The code in your previous question was correct but it misses something important or you haven't added that code to the question.
Here is a demo plugin that can help you get it working.
This Plugin demonstrates how you can build your own plugin pages using the WordPress provided draggable metaboxes, requires WordPress 2.7 version, supports WordPress 2.8 changed boxing layout engine
The basic code from the demo plugin is the following. Note that in the full exemple the side meta boxes are not working nor the two columns layout as it was written for WordPress 2.8 and current version is almost 5.0.
// Source code by Frank Bueltge at gist.github.com/bueltge/757903
class howto_metabox_plugin {
function howto_metabox_plugin() {
add_action('admin_menu', array($this, 'on_admin_menu'));
add_action('admin_post_save_howto_metaboxes_general', array($this, 'on_save_changes'));
}
function on_admin_menu() {
$this->pagehook = add_options_page('Howto Metabox Page Title', "HowTo Metaboxes", 'manage_options', 'howto_metaboxes', array($this, 'on_show_page'));
add_action('load-'.$this->pagehook, array($this, 'on_load_page'));
}
function on_load_page() {
wp_enqueue_script('common');
wp_enqueue_script('wp-lists');
wp_enqueue_script('postbox');
add_meta_box('howto-metaboxes-contentbox-2', 'Contentbox 2 Title', array($this, 'on_contentbox_2_content'), $this->pagehook, 'normal', 'core');
add_meta_box('howto-metaboxes-contentbox-additional-1', 'Contentbox Additional 1 Title', array($this, 'on_contentbox_additional_1_content'), $this->pagehook, 'additional', 'core');
}
function on_show_page() {
//define some data can be given to each metabox during rendering
$data = array('My Data 1', 'My Data 2', 'Available Data 1');
?>
<div id="howto-metaboxes-general" class="wrap">
<?php screen_icon('options-general'); ?>
<h2>Metabox Showcase Plugin Page</h2>
<form action="admin-post.php" method="post">
<?php wp_nonce_field('howto-metaboxes-general'); ?>
<input type="hidden" name="action" value="save_howto_metaboxes_general" />
<div id="poststuff" class="metabox-holder">
<div id="side-info-column" class="inner-sidebar">
<?php do_meta_boxes($this->pagehook, 'side', $data); ?>
</div>
<div id="post-body" class="has-sidebar">
<div id="post-body-content" class="has-sidebar-content">
<?php do_meta_boxes($this->pagehook, 'normal', $data); ?>
<?php do_meta_boxes($this->pagehook, 'additional', $data); ?>
<p>
<input type="submit" value="Save Changes" class="button-primary" name="Submit"/>
</p>
</div>
</div>
<br class="clear"/>
</div>
</form>
</div>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
// close postboxes that should be closed
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
// postboxes setup
postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
});
//]]>
</script>
<?php
}
function on_save_changes() {
if ( !current_user_can('manage_options') )
wp_die( __('Cheatin’ uh?') );
check_admin_referer('howto-metaboxes-general');
//process here your on $_POST validation and / or option saving
//lets redirect the post request into get request (you may add additional params at the url, if you need to show save results
wp_redirect($_POST['_wp_http_referer']);
}
function on_contentbox_2_content($data) {
sort($data);
?>
<p>The given parameter at <b>reverse sorted</b> order are: <em><?php echo implode(' | ', array_reverse($data)); ?></em></p>
<?php
}
function on_contentbox_additional_1_content($data) {
?>
<p>This and the 2nd <em>additional</em> box will be addressed by an other group identifier to render it by calling with this dedicated name.</p>
<p>You can have as much as needed box groups.</p>
<?php
}
}
$my_howto_metabox_plugin = new howto_metabox_plugin();

Wordpress Custom Value If/ElseIf - Doesn't Work

I'm outside the loop and want to call a custom value "featvideo" and display it. If there isn't "featvideo" then print an image...
The video displays, but when there isn't a video a blank box displays. You can see the the issue here: http://wgl.buildthesis.com
(and yes, $images is a function defined in functions.php and works)
<?php
$feat_catbox_1 = new WP_Query("cat=$tt_feat_id&showposts=$tt_feat_postcount");
while ($feat_catbox_1->have_posts()) : $feat_catbox_1->the_post();
$key = 'featvideo';
$video_url = get_post_custom_values($key);
$featuredvideo = $video_url[0];
?>
<div class="contentdiv">
<div id="featured-thumb">
<?php if ($key=="featvideo")
echo $featuredvideo;
elseif ($key=="")
echo $images('1', '390', '244', 'alignleft', true); ?>
</div>
</div>
Since you have set $key = 'featvideo' and then test if it is set later, it will always return true. You never change the value of $key anywhere in your code except when you set it.
I would suggest something like the following for your if statement:
<?php
if($featuredvideo)
echo $featuredvideo;
else
echo $images('1', '390', '244', 'alignleft', true);
?>

Resources