我有一个与棒球相关的网站,有多名作者。我用“把这篇文章贴在头版”来表示文章的“编辑选择”。我想添加一个链接/按钮,允许编辑器从前端执行此操作。该方法可以在文章本身中,也可以在管理栏中。我真的没有偏好。
我浏览了很多不同的“管理栏”插件,但没有找到任何与“将此帖子贴到首页”相关的内容。
谢谢
我有一个与棒球相关的网站,有多名作者。我用“把这篇文章贴在头版”来表示文章的“编辑选择”。我想添加一个链接/按钮,允许编辑器从前端执行此操作。该方法可以在文章本身中,也可以在管理栏中。我真的没有偏好。
我浏览了很多不同的“管理栏”插件,但没有找到任何与“将此帖子贴到首页”相关的内容。
谢谢
我认为这个小源代码就是您的解决方案。它目前没有粘性贴子更改的前端反馈,但您可以在函数中增强此字符串、类或任何您想要的内容fb_stick_post
.
第一个函数在管理栏中添加该项,并使用param值创建一个新Url。而且on click
调用函数读取Url参数,如果为true,则更改此帖子id的粘性状态。
add_action( \'admin_bar_menu\', \'fb_add_admin_bar_sticky\', 35 );
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 )
) {
$wp_admin_bar->add_menu(
array(
\'id\' => \'sticky_post\',
\'title\' => __(\'Sticky\'),
\'href\' => get_permalink() . \'?stick_post=true\',
\'meta\' => array(
\'title\' => __( \'Click me\' ),
\'onclick\' => fb_stick_post( get_the_ID() )
)
)
);
}
}
function fb_stick_post( $post_id ) {
if ( isset($_GET[\'stick_post\']) && \'true\' == htmlspecialchars( $_GET[\'stick_post\'] ) )
stick_post( $post_id );
}
Update 07/30/2012
现在是一个简单解决方案的小插件。插件在管理栏中添加了一个项目。按钮检查的字符串是粘性的,可以粘贴或取消粘贴当前帖子。我将用于添加管理栏项目的挂钩更改为“template\\u redirect”,以便在更新post上的stick标志后使用重定向。
<?php
/**
* Plugin Name: Stick/Unstick post via Admin bar
*
*/
if ( ! function_exists( \'fb_add_admin_bar_sticky\' ) ) {
add_action( \'template_redirect\', \'fb_add_admin_bar_sticky\' );
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() )
)
)
);
}
}
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();
}
}
}
或下载此插件Gist 3214922这里有几个有用的功能:
unstick_post
- 取消粘贴帖子stick_post
- 贴帖子is_sticky
- 弄清楚一篇文章是否有粘性,记住这三点,我们所需要做的就是用一些管理菜单栏胶水将它们粘在一起。首先,让我们把所有的东西都放在一个班级里,以获得乐趣和收益。这个类将有一些我们稍后将使用的常量:nonce、取消粘贴帖子的操作和粘贴帖子的操作。
class WPSE_58818_Stick_Post
{
/**
* Ajax nonce.
*
* @since 1.0
*/
const NONCE = \'wpse58818_nonce_\';
/**
* Unstick ajax action
*
* @since 1.0
*/
const UNSTICK = \'wpse58818_unstick\';
/**
* Stick Ajax action
*
* @since 1.0
*/
const STICK = \'wpse58818_stick\';
} // end class
然后,让我们添加一个init函数来添加我们的操作。第一个操作是挂接到template\\u redirect。<?php
class WPSE_58818_Stick_Post
{
// snip snip
/**
* Adds actions and such.
*
* @since 1.0
* @access public
* @uses add_action
*/
public static function init()
{
add_action(
\'template_redirect\',
array(__CLASS__, \'template_r\')
);
}
}
注意:从现在起,我将省略class
一点您可以查看整个内容here.在连接到的函数中template_redirect
, 我们将检查是否位于单个贴子页面上,以及用户是否可以编辑它。如果可以的话,我们会admin_bar_menu
和wp_footer
.
/**
* Hooked into `template_redirect`. Adds the admin bar stick/unstick
* button if we\'re on a single post page and the current user can edit
* the post
*
* @since 1.0
* @access public
* @uses add_action
*/
public static function template_r()
{
if(
!is_single() ||
!current_user_can(\'edit_post\', get_queried_object_id())
) return; // not a single post or the user can\'t edit it
// Hook into admin_bar_menu to add stuff
add_action(
\'admin_bar_menu\',
array(__CLASS__, \'menu\'),
100
);
// Hook into the footer and spit out some JavaScript
add_action(
\'wp_footer\',
array(__CLASS__, \'footer\')
);
}
在menu
函数,连接到admin_bar_menu
, 我们可以添加新项目:/**
* Hooked into `admin_bar_menu`. Adds our stick/unstick node.
*
* @since 1.0
* @access public
*/
public static function menu($mb)
{
// get the current post ID
$post_id = get_queried_object_id();
$mb->add_node(array(
\'id\' => \'wpse58818-sticker\',
\'meta\' => array(
\'class\' => \'wpse58818-sticker\',
\'title\' => is_sticky($post_id) ? \'unstick\' : \'stick\'
),
\'title\' => is_sticky($post_id) ? __(\'Unstick\') : __(\'Stick\'),
\'href\' => self::get_url($post_id)
));
}
这里我们得到了第一个“实用程序函数”,它为管理菜单栏节点构建URL。这只是个包装add_query_arg
然后构建一个url,稍后将与AJAX一起使用:/**
* Get an Ajax URL to use for a given post
*
* @since 1.0
* @access protected
*/
protected static function get_url($post_id)
{
return add_query_arg(array(
\'post_id\' => absint($post_id),
\'action\' => is_sticky($post_id) ? self::UNSTICK : self::STICK,
\'nonce\' => wp_create_nonce(self::NONCE . $post_id)
), admin_url(\'admin-ajax.php\'));
}
Thefooter
函数只是输出一些JavaScript来进行AJAX调用。基本概述:当有人单击我们的新链接时,对给定的URL发出GET请求。如果成功,请更改(取消)粘贴链接的href、文本和标题。/**
* Hooked into `wp_footer`. Spits out a bit of JS to stick/unstick a post
*
* @since 1.0
* @access public
*/
public static function footer()
{
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(\'.wpse58818-sticker a\').on(\'click\', function(e) {
e.preventDefault();
var action = $(this).attr(\'title\');
var that = this;
$.get(
$(this).attr(\'href\'),
{},
function(data) {
if(\'0\' == data)
{
console.log(data);
alert(\'<?php echo esc_js(__(\'An error occurred\')); ?>\');
return;
}
$(that).attr(\'href\', data);
if(\'stick\' == action) {
$(that).html(\'<?php echo esc_js(__(\'Unstick\')); ?>\');
$(that).attr(\'title\', \'unstick\');
} else {
$(that).html(\'<?php echo esc_js(__(\'Stick\')); ?>\');
$(that).attr(\'title\', \'stick\');
}
}
);
});
});
</script>
<?php
}
现在我们来看AJAX回调。Ajax in plugins/themes 值得一读。我们将修改init
稍微增加一点功能,再添加两个操作:
/**
* Adds actions and such.
*
* @since 1.0
* @access public
* @uses add_action
*/
public static function init()
{
add_action(
\'template_redirect\',
array(__CLASS__, \'template_r\')
);
// Ajax actions
add_action(
\'wp_ajax_\' . self::STICK,
array(__CLASS__, \'stick\')
);
add_action(
\'wp_ajax_\' . self::UNSTICK,
array(__CLASS__, \'unstick\')
);
}
以及我们的AJAX回调。这些很可能是相同的功能。我在这里把它们分开,因为在将来似乎更容易扩展/更改。这两种方法都会检查AJAX请求是否有效,(取消)相应地粘贴帖子,并回显新的URL以备将来(取消)粘贴。/**
* Ajax callback for the stick function
*
* @since 1.0
* @access public
*/
public static function stick()
{
$post_id = self::can_ajax();
stick_post($post_id);
echo self::get_url($post_id);
die();
}
/**
* Ajax callback for the unstick function
*
* @since 1.0
* @access public
* @uses unstick_post
*/
public static function unstick()
{
$post_id = self::can_ajax();
// nonces checked, everything is good to go. Unstick!
unstick_post($post_id);
echo self::get_url($post_id);
die();
}
我们的第二个“效用函数”出现在这里。can_ajax
验证nonce和用户权限,并将post ID返回(取消)stick。如果任何检查失败,它将退出(通过die(\'1\')
)./**
* Check to see if the current user can ajax. Returns the post ID to
* stick/unstick if successful. Kills the program otherwise
*
* @since 1.0
* @access protected
*/
protected static function can_ajax()
{
$post_id = isset($_REQUEST[\'post_id\']) ? $_REQUEST[\'post_id\'] : \'\';
if(
!$post_id ||
!check_ajax_referer(self::NONCE . $post_id, \'nonce\', false)
) die(\'0\');
if(!current_user_can(\'edit_post\', $post_id))
die(\'0\');
return $post_id;
}
这就是整个mess as a plugin.这里有一个更简单的解决方案,可以使用the_content
过滤器挂钩
add_filter(\'the_content\',\'simplest_sticky_solution\');
function simplest_sticky_solution($content){
global $post;
//early exit if not needed
if (!is_single() || !current_user_can(\'edit_post\',$post->ID))
return $content;
//check if form is submitted and act as needed
if (isset($_POST[\'sticky_action\']) && isset($_POST[\'sticky_id\']) && isset($_POST[\'sticky_nonce\']) && wp_verify_nonce($_POST[\'sticky_nonce\'], \'StickIt\')){
if (is_sticky($post->ID)){
stick_post($post->ID);
}else{
unstick_post($post->ID);
}
}
//create the form
$label = (is_sticky())? "Unstick": "Stick";
$form = \'
<form action="" method="POST">
<input type="hidden" name="sticky_id" value="\'.$post->id.\'">
<input type="hidden" name="sticky_action" value="stickit">
<input type="hidden" name="sticky_nonce" value="\'.wp_create_nonce(\'StickIt\').\'">
<input type="button" name="submit" value="\'.$label.\'">
</form>\';
return $form.\'<br/>\'.$content;
}
我使用以下代码生成wp_editor 实例:<?php if (version_compare($wp_version, \"3.3\") >= 0) { ?> <p><?php wp_editor( $answer->post_content, \'answer\', array( \'media_buttons\' => true, \'wpautop\' => true ) ); ?></p> <