如何向:‘wp-admin/post-new.php’添加类别?

时间:2012-12-28 作者:A T

我想有一个链接,以创建一个新的职位,设置类别也。

我试过了wp-admin/post-new.php?post_category=12wp-admin/post-new.php?cat=12, 但两者都不起作用。我还尝试使用名称而不是类别的id;这也没有影响。

How do I create a link to a new post with a default category?

4 个回复
最合适的回答,由SO网友:user67014 整理而成

GitHub的Dave James Miller帮我搞定了这个。这些工作都不是我做的,我只是把他的代码打包在一个plguin中,因为它和广告中的一样完美:

<?php
/**
 * Plugin Name: Set default category from url parameter
 * Plugin URI:  https://gist.github.com/davejamesmiller/1966543
 * Description: enables you to setup new post links with the post_title, category and tags in the url: <code><a href="<?= esc_attr(admin_url(\'post-new.php?post_title=Default+title&category=category1&tags=tag1,tag2\')) ?>">New post</a></code>
 * Version:     0.0.1
 * Author:      davejamesmiller
 * Author URI:  https://gist.github.com/davejamesmiller
 */

// I used this code to automatically set the default post title, category and
// tags for a new WordPress post based on which link was clicked. It could also
// be tweaked to hard-code the values instead of using request parameters.


add_filter(\'wp_get_object_terms\', function($terms, $object_ids, $taxonomies, $args)
{
    if (!$terms && basename($_SERVER[\'PHP_SELF\']) == \'post-new.php\') {

        // Category - note: only 1 category is supported currently
        if ($taxonomies == "\'category\'" && isset($_REQUEST[\'category\'])) {
            $id = get_cat_id($_REQUEST[\'category\']);
            if ($id) {
                return array($id);
            }
        }

        // Tags
        if ($taxonomies == "\'post_tag\'" && isset($_REQUEST[\'tags\'])) {
            $tags = $_REQUEST[\'tags\'];
            $tags = is_array($tags) ? $tags : explode( \',\', trim($tags, " \\n\\t\\r\\0\\x0B,") );
            $term_ids = array();
            foreach ($tags as $term) {
                if ( !$term_info = term_exists($term, \'post_tag\') ) {
                    // Skip if a non-existent term ID is passed.
                    if ( is_int($term) )
                        continue;
                    $term_info = wp_insert_term($term, \'post_tag\');
                }
                $term_ids[] = $term_info[\'term_id\'];
            }
            return $term_ids;
        }
    }
    return $terms;
}, 10, 4);

SO网友:fuxia

钩入wp_insert_post, 测试的post状态auto-draft, 以及GET 参数

但首先我们需要一个helper函数来获取和清理GET 参数:

/**
 * Set default category.
 *
 * @wp-hook pre_option_default_category
 * @return  string Category slug
 */
function t5_get_default_cat_by_url()
{
    if ( ! isset( $_GET[\'post_cat\'] ) )
        return FALSE;

    return array_map( \'sanitize_title\', explode( \',\', $_GET[\'post_cat\'] ) );
}
现在,自动拔模处理程序:

add_action( \'wp_insert_post\', \'t5_draft_category\', 10, 2 );

/**
 * Add category by URL parameter to auto-drafts.
 *
 * @wp-hook wp_insert_post
 * @param   int $post_ID
 * @param   object $post
 * @return  WP_Error|array An error object or term ID array.
 */
function t5_draft_category( $post_ID, $post )
{
    if ( ! $cat = t5_get_default_cat_by_url()
        or \'auto-draft\' !== $post->post_status )
        return;

    // return value will be used in unit tests only.
    return wp_set_object_terms( $post_ID, $cat, \'category\' );
}
只有当get_default_post_to_edit() 使用第二个参数调用$create_in_db 设置为TRUE. 要捕获其他情况,您必须过滤选项default_category:

add_filter( \'pre_option_default_category\', \'t5_get_default_cat_by_url\' );
现在可以使用参数post_cat 要传递以逗号分隔的类别段塞列表,请执行以下操作:

enter image description here

另请参见:

SO网友:bueltge

我想你可以选择默认选项default_category 和过滤器option_default_category 如果url具有类别的参数,如下面的示例源。使用它作为插件,测试它。从头开始写入,未测试。

url参数为post_cat 您可以设置类别,如以下url:/wp-admin/post-new.php?post_cat=categoryname

<?php
/**
 * Plugin Name: .my Test
 * Plugin URI:  http://bueltge.de/
 * Description: 
 * Version:     0.0.1
 * Author:      Frank B&uuml;ltge
 * Author URI:  http://bueltge.de/
 */
class Set_Default_Cat_From_Url_Param {

    protected static $classobj = NULL;

    public static function init() {

        NULL === self::$classobj and self::$classobj = new self();

        return self::$classobj;
    }

    function __construct() {

        if ( isset( $_GET[\'post_cat\'] ) )
            add_filter( \'option_default_category\', array( $this, \'get_category\' ) );
    }

    function get_category( $category ) {

        if ( isset( $_GET[\'post_cat\'] ) )
            $category = get_cat_ID( esc_attr( $_GET[\'post_cat\'] ) );

        return $category;
    }

}
add_action( \'load-post-new.php\', array( \'Set_Default_Cat_From_Url_Param\', \'init\' ) );

SO网友:Malcalevak

我意识到这已经得到了回答,但我想补充我自己的看法。我已经把它添加到这里的要点中了https://gist.github.com/malcalevak/ba05b4fbed0c6e33ac8c18c1451bd857

不过,为了省去麻烦,下面是代码:

function set_category () {

    global $post;
  //Check for a category parameter in our URL, and sanitize it as a string
    $category_slug = filter_input(INPUT_GET, \'category\', FILTER_SANITIZE_STRING, array("options" => array("default" => 0)));

  //If we\'ve got a category by that name, set the post terms for it
    if ( $category = get_category_by_slug($category_slug) ) {
        wp_set_post_terms( $post->ID, array($category->term_id), \'category\' );
    }

}

//hook it into our post-new.php specific action hook
add_action( \'admin_head-post-new.php\', \'set_category\', 10, 1 );
与所有其他类似,您可以通过/wp admin/post new触发它。php?类别=类别名称

仅供参考,如果您正在使用高级自定义字段,如@Aphire,这将起作用。

结束