将预定义/缺省值添加到自定义发布插件

时间:2016-04-13 作者:Thomas.D

我已经创建了一个自定义贴子插件。

在添加新的自定义帖子时,我需要在此插件中添加预定义的文本/值。

是否可以在我的插件PHP文件中处理这些预定义的文本/值,并将其激活?

/*
Plugin Name: Grand Prix Autíčka
Plugin URI: http://www/
Description: Přidávání autíček
Version: 1.0
Author: Thomas Dobo
Author URI: http://www/
License: GPLv2
*/

add_action( \'init\', \'create_movie_review\' );

function create_movie_review() {
    register_post_type(\'movie_reviews\', array(
        \'labels\' => array(
            \'name\' => \'Grand Prix Autíčka\',
            \'singular_name\' => \'Movie Review\',
            \'add_new\' => \'Přidat Nový\',
            \'add_new_item\' => \'Add New Movie Review\',
            \'edit\' => \'Upravit\',
            \'edit_item\' => \'Edit Movie Review\',
            \'new_item\' => \'New Movie Review\',
            \'view\' => \'View\',
            \'view_item\' => \'View Movie Review\',
            \'search_items\' => \'Search Movie Reviews\',
            \'not_found\' => \'No Movie Reviews found\',
            \'not_found_in_trash\' =>
            \'No Movie Reviews found in Trash\',
            \'parent\' => \'Parent Movie Review\'
        ),
        \'public\' => true,
        \'menu_position\' => 15,
        \'supports\' =>
        array(\'title\', \'editor\', \'comments\',
            \'thumbnail\',),
        \'taxonomies\' => array(\'\'),
        \'menu_icon\' =>
        plugins_url(\'images/image.png\', __FILE__),
        \'has_archive\' => true
            )
    );
}
add_action( \'admin_init\', \'my_admin\' );
我添加了此代码,但不起作用I need default content only for plugins new post

function my_editor_content( $content ) {
    $content = "";    
    if ( \'movie_reviews\' == get_post_type() ) {
        $content = "This is some custom content I\'m adding to the post editor because I hate re-typing it."; 
    }

    return $content;
}
请告知

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

您可以使用default_content 仅在添加新帖子屏幕上过滤默认帖子内容的过滤器。

示例:

function filter_function_name( $content, $post ) {
    if ( \'movie_reviews\' == get_post_type($post) && empty($content) ) {
        $content = "This is some custom content I\'m adding to the post editor because I hate re-typing it.";
    }
    return $content;
}
add_filter( \'default_content\', \'filter_function_name\', 10, 2 );

相关推荐