Created
July 31, 2012 08:25
-
-
Save bueltge/3214922 to your computer and use it in GitHub Desktop.
WordPress Plugin, that add a stick/unstick post link to the admin bar of WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Stick/Unstick post via Admin bar | |
* Plugin URI: http://wordpress.stackexchange.com/q/58818/6035 | |
* Description: Add a stick/unstick post link to the admin bar of WordPress. | |
* Version: 1.0.0 | |
* Author: Frank Bültge | |
* Author URI: http://bueltge.de | |
* License: GPLv3 | |
*/ | |
// This file is not called by WordPress. We don't like that. | |
! defined( 'ABSPATH' ) and exit; | |
// works only, if the function is not active | |
if ( ! function_exists( 'fb_add_admin_bar_sticky' ) ) { | |
/** | |
* Include function in WP | |
* Use template redirect for sure works of the redirect after change sticky flag | |
*/ | |
add_action( 'template_redirect', 'fb_add_admin_bar_sticky' ); | |
/** | |
* Add item in Admin Bar of WP | |
* Create different strings for stick,unstick in this menu item | |
* Create the url param and call function to change flag on sticky | |
* | |
* @param void | |
* @return void | |
*/ | |
function fb_add_admin_bar_sticky() { | |
global $wp_admin_bar; | |
if ( ! is_super_admin() || ! is_admin_bar_showing() ) | |
return; | |
$current_object = get_queried_object(); | |
if ( empty($current_object) ) | |
return; | |
if ( ! empty( $current_object->post_type ) && | |
( $post_type_object = get_post_type_object( $current_object->post_type ) ) && | |
current_user_can( $post_type_object->cap->edit_post, $current_object->ID ) | |
) { | |
// check, if an sticky post | |
if ( is_sticky( get_the_ID() ) ) { | |
$title = __('Unsticky'); | |
$link = '?unstick_post=true'; | |
$attr_title = __( 'Make this post unsticky' ); | |
} else { | |
$title = __('Sticky'); | |
$link = '?stick_post=true'; | |
$attr_title = __( 'Make this post sticky' ); | |
} | |
$wp_admin_bar->add_menu( | |
array( | |
'id' => 'sticky_post', | |
'title' => $title, | |
'href' => get_permalink() . $link, | |
'meta' => array( | |
'title' => $attr_title, | |
'onclick' => fb_stick_post( get_the_ID() ) | |
) | |
) | |
); | |
} | |
} | |
/** | |
* Stick, unstick post, if the url param have the right value | |
* Redirect to the current post ID | |
* | |
* @param Integer $post_id ID of Post | |
* @return void | |
*/ | |
function fb_stick_post( $post_id ) { | |
if ( isset($_GET['stick_post']) && 'true' == htmlspecialchars( $_GET['stick_post'] ) ) { | |
stick_post( $post_id ); | |
wp_redirect( get_permalink( $post_id ) ); | |
exit(); | |
} | |
if ( isset($_GET['unstick_post']) && 'true' == htmlspecialchars( $_GET['unstick_post'] ) ) { | |
unstick_post( $post_id ); | |
wp_redirect( get_permalink( $post_id ) ); | |
exit(); | |
} | |
} | |
} // end if function_exists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment