向管理员的帖子页面添加存档按钮

时间:2013-05-27 作者:t3hn00b

我想在Edit, Quick Edit, Trash and View 帖子页面中的按钮-http://i.imgur.com/HDZBPCy.png. 我发现了一个很好的函数,可能可以做到这一点,但我不知道如何使用它-wp_set_post_categories. 我想要的是有一个静电Archive 类别,这在任何菜单中都不存在,但由于网站的性质(大量订单、结果等),我们必须保留它们,我只是不希望它们在截止日期到期后被丢弃或取消发布。我翻阅了这几页,想知道Delete 按钮可以工作,但我已经找到了30多个引用,我不知道它到底是如何工作的。任何帮助都将不胜感激。

1 个回复
SO网友:Bainternet

以下是我以前使用过的一种更改帖子类别(或任何术语/分类法)的解决方案,请确保设置正确$post_type,$append,$taxonomy,$terms

<?php
/*
Plugin Name: Simple Ajax set cat
Plugin URI: http://en.bainternet.info
Description: dead simple lightweight plugin to add a post row action link to set a post category by ajax
Version: 0.1
Author: Bainternet
Author Email: [email protected]
License:

  Copyright 2013 Bainternet ([email protected])

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2, as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
/**
* SimpleCat
* @author Ohad Raz <[email protected]>
*/
class SimpleArchive
{
    public $post_type = \'post\';
    public $append    = false;
    public $taxonomy  = \'category\';
    public $terms     = array(1); //you can change this to string of term names

    /**
     * Class constructor
     *
     * @author Ohad Raz <[email protected]>
     * @since 0.1
     * @param array $args optional for feuture
     */
    function __construct($args = array()){
        //add row action link
        add_filter(\'post_row_actions\',array($this,\'_action_row\'), 10, 2);
        //add javascript fp ajax call
        add_action(\'admin_footer-edit.php\',array($this,\'addJS\'));
        //ajax function
        add_action(\'wp_ajax_setToCat\', array($this,\'ajaxSetToCat\'));
    }
    /**
     * adds an action link to post listing row
     * @author Ohad Raz <[email protected]>
     * @since 0.1
     * @param  array  $actions row actions
     * @param  onject $post    Post object
     * @return array modified actions
     */
    function _action_row($actions, $post){
        //check for your post type
        if ($post->post_type == $this->post_type){
            $actions[\'archive\'] = \'<a href="#" class="move_TO_CAT" data-pid="\'.$post->ID.\'">\'.__(\'Archive\').\'</a>\';
        }
        return $actions;
    }

    /**
     * Adds the javascript magic to make the ajax request
     * 
     * @author Ohad Raz <[email protected]>
     * @since 0.1
     * @return void
     */
    function addJS(){
        wp_enqueue_script( \'jquery\');
        ?>
         <div id="status_update_working" style="background-color: green; color: #fff; font-wieght: bolder;   font-size: 22px;   height: 33px;   left: 40%;   padding: 35px;   position: fixed;   top: 100px;   width: 350px; display:none !important; "><?php _e(\'Changing status...\'); ?></div>
        <script type="text/javascript">
        jQuery(document).ready(function($){
            function ajaxSetCat(p){
                jQuery("#status_update_working").show(\'fast\');
                jQuery.getJSON(ajaxurl,
                    {   pid: p.attr("data-pid"),
                        action: "setToCat",
                        _nonce: "<?php echo wp_create_nonce(\'setToCat\'); ?>"
                    },
                    function(data) {
                        if (data.error){
                            alert(data.error);
                        }else{
                             alert(data.text);
                        }
                    }
                );
                jQuery("#status_update_working").hide(\'9500\');
            }
            $(\'.move_TO_CAT\').click(function(){
                ajaxSetCat($(this))
            });
        });
        </script>
        <?php

    }

    /**
     * ajax callback function to actually set the category
     * 
     * @author Ohad Raz <[email protected]>
     * @since 0.1
     * @return void
     */
    function ajaxSetToCat(){
        //minor validation
        if (!isset($_GET[\'pid\']) || ! wp_verify_nonce($_GET[\'_nonce\'], \'setToCat\')){
            $re[\'error\'] = __(\'something went wrong ...\');
            echo json_encode($re);
            die();
        }
        $results = wp_set_post_terms( intval($_GET[\'pid\']), $this->terms, $this->taxonomy, $this->append );
        if ( is_wp_error( $results ) ){
            $re[\'error\'] = __(\'something went wrong ...\') ." ". $results->get_error_message();
        }elseif($results === false || !is_array($results)){
            $re[\'error\'] = __(\'something went wrong ...\');
        }else{
            $re[\'text\'] = __(\'Set to cat successful\');
        }
        echo json_encode($re);
        die();
    }
}//end class
new SimpleArchive();

结束

相关推荐

Number of displayed posts

我已经尝试了很多方法来解决这个问题。所以我有一个插件,它在一个小部件中显示分配给用户的项目。它将项目显示为帖子。问题是它只显示5 页面上的项目。我正在发送一个显示帖子的代码:$projects = get_posts(array(\'post_type\' => \'projects\')); 有个人叫我把它改成$projects = get_posts(array(\'post_type\' => \'projects\', \'number posts\' => 10));