tpw_complete': // 重置密码 $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': // 我的首页评论 $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': // 单页 $pre .= $default_pre .= 'single_page.htm'; break; case 'search': // 搜索 $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': // 置顶 $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': // 关闭 $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': // 删除 $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': // 移动 $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: // 首页 $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } // 加载绑定ID安装风格 !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; // 加载安装风格 (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; // 主风格下可安装多个子风格 if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; // 加载绑定ID安装风格 $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; // 加载安装风格 !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } // 风格不存在加载适配端 !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } // 依据模式返回适配文件 function theme_mode_pre($type = 0) { global $config; // 网站模式 $mode = $config['setting']['website_mode']; $pre = ''; // 首页文件前缀 if (1 == $mode) { // 门户模式 $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { // 扁平模式 $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { // 自定义模式 $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>categories - How to add category to: 'wp-adminpost-new.php'?|Concepts Of Algorithm

categories - How to add category to: 'wp-adminpost-new.php'?

admin2025-06-04  0

I want to have a link to create a new post that sets the category also.

I have tried wp-admin/post-new.php?post_category=12 and wp-admin/post-new.php?cat=12, but neither worked. I also tried using the name rather than the id of the category; which also had no affect.

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

I want to have a link to create a new post that sets the category also.

I have tried wp-admin/post-new.php?post_category=12 and wp-admin/post-new.php?cat=12, but neither worked. I also tried using the name rather than the id of the category; which also had no affect.

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

Share Improve this question asked Dec 28, 2012 at 11:30 A TA T 2263 silver badges13 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

Dave James Miller over at GitHub nailed this one for me. None of the work is from me, I'm just posting his code wrapped into a plguin since it works perfectly as advertised:

<?php
/**
 * Plugin Name: Set default category from url parameter
 * Plugin URI:  https://gist.github/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/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);

Hook into wp_insert_post, test the post status for auto-draft, and the URL for a GET parameter.

But first we need a helper function to get and sanitize the GET parameter:

/**
 * 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'] ) );
}

Now the auto-draft handler:

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' );
}

This works only if get_default_post_to_edit() was called with the second parameter $create_in_db set to TRUE. To catch the other case you have to filter the option default_category:

add_filter( 'pre_option_default_category', 't5_get_default_cat_by_url' );

Now you can use the parameter post_cat to pass a comma separated list of category slugs:

See also:

  • Template plugin for blog posts?
  • Open Wordpress 'Add New Post' admin page with parameters set via $_GET
  • Force category choice before creating new post?

I think you can go about the default option default_category and filter option_default_category this, if the url have a param for the category, like this example source. Use it as plugin, test it. Was write from scratch and not tested.

The url param is post_cat and you can set the category, like this 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' ) );

I realize this has already been answered, but I wanted to add my own take. I've added it to a gist here https://gist.github/malcalevak/ba05b4fbed0c6e33ac8c18c1451bd857

To save you the hassle, though, here's the code:

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 );

Similar to all the others, you'd trigger it via /wp-admin/post-new.php?category=categoryname

FYI, if you're using Advanced Custom Fields, like @Aphire, this WILL work.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749050005a315928.html

lang[new_post](0)