【问题标题】:Best way to replace page text in WordPress在 WordPress 中替换页面文本的最佳方法
【发布时间】:2020-05-01 05:38:20
【问题描述】:

我找到了两种解决方案来替换 WordPress 页面中的文本。两者都“几乎”正常工作,除了一些问题:

解决方案 1:

function callback($buffer) {
  // modify buffer here, and then return the updated code
  return $buffer;
}

function buffer_start() { ob_start("callback"); }

function buffer_end() { ob_end_flush(); }

add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');

取自:

WordPress filter to modify final html output

http://www.dagondesign.com/articles/wordpress-hook-for-entire-page-using-output-buffering/

它工作正常,但不允许更改页面标题。有什么办法可以改变上述方法中的页面标题?

我从一个流行的插件(代码行号 270)得到的第二个解决方案:https://wordpress.org/plugins/real-time-find-and-replace/

function far_template_redirect() {
    ob_start();
    ob_start( 'far_ob_call' );
    //ob_end_flush(); fails here. I don't know why?
}

//Handles find and replace for public pages
add_action( 'template_redirect', 'far_template_redirect' );

它工作正常,但许多在线链接建议使用add_filter 'template_include' 而不是add_action 'template_redirect'。见:

https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/ https://bbpress.trac.wordpress.org/ticket/1524

但如果我尝试使用add_action 'template_redirect' 而不是add_filter 'template_include',文本替换代码在某些网站上有效并破坏了其他一些网站。

人们建议ob_end_flush() 必须与ob_start() 一起使用,但是如果我在ob_start 之后的代码中包含ob_end_flush,则代码会失败。

我喜欢solution 1,但有没有办法在其中包含页眉和页脚?否则Solution 2 适用于所有网站,除了它没有ob_end_flush 并且不被很多人推荐,尽管一个流行的插件使用它。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    function custom_resource_type() {
    
        $labels = array(
            'name'                => _x( 'Resources', 'Post Type General Name', 'gucherry-blog' ),
            'singular_name'       => _x( 'Resource', 'Post Type Singular Name', 'gucherry-blog' ),
            'menu_name'           => __( 'Resources', 'gucherry-blog' ),
            'parent_item_colon'   => __( 'Parent Resource', 'gucherry-blog' ),
            'all_items'           => __( 'All Resources', 'gucherry-blog' ),
            'view_item'           => __( 'View Resource', 'gucherry-blog' ),
            'add_new_item'        => __( 'Add New Resource', 'gucherry-blog' ),
            'add_new'             => __( 'Add Resource', 'gucherry-blog' ),
            'edit_item'           => __( 'Edit Resource', 'gucherry-blog' ),
            'update_item'         => __( 'Update Resource', 'gucherry-blog' ),
            'search_items'        => __( 'Search Resource', 'gucherry-blog' ),
            'not_found'           => __( 'Not Found', 'gucherry-blog' ),
            'not_found_in_trash'  => __( 'Not found in Trash', 'gucherry-blog' ),
        );
    
        $args = array(
            'label'               => __( 'resources', 'gucherry-blog' ),
            'description'         => __( 'Resource for software products and online', 'gucherry-blog' ),
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
            'taxonomies'          => array( '' ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'menu_icon'           => 'dashicons-admin-site-alt2',
            'capability_type'     => 'page',
        );
    
        register_post_type( 'resources', $args );
    
         $resource_cats = array(
        'name' => _x( 'Resource Categories', 'taxonomy general name', 'gucherry-blog' ),
        'singular_name' => _x( 'Resource Category', 'taxonomy singular name', 'gucherry-blog' ),
        'search_items' =>  __( 'Search Resource Categories', 'gucherry-blog' ),
        'all_items' => __( 'All Resource', 'gucherry-blog' ),
        'parent_item' => __( 'Parent Resource', 'gucherry-blog' ),
        'parent_item_colon' => __( 'Parent Resource:', 'gucherry-blog' ),
        'edit_item' => __( 'Edit Resource', 'gucherry-blog' ), 
        'update_item' => __( 'Update Resource', 'gucherry-blog' ),
        'add_new_item' => __( 'Add New Resource', 'gucherry-blog' ),
        'new_item_name' => __( 'New Resource', 'gucherry-blog' ),
        'menu_name' => __( 'Resource Categories', 'gucherry-blog' ),
      );    
    
      register_taxonomy('resource_categories',array('resources'), array(
        'hierarchical' => true,
        'labels' => $resource_cats,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'resource' ),
      ));
    
    }

    【讨论】:

    猜你喜欢
    • 2011-02-19
    • 2015-10-09
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2012-07-30
    • 2010-09-06
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多