1

I found two solutions to replace text in WordPress page. Both are working "almost" fine, except for a few issues:

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

It is taken from:

WordPress filter to modify final html output and

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

It works fine but it does not allow change of page title. Is there any way to change page title in above method?

The second solution I got from a popular plugin (code line no. 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' );

It works fine, but many links online suggest that add_filter 'template_include' should be used instead of add_action 'template_redirect'. See:

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

But if I try add_action 'template_redirect' instead of add_filter 'template_include', the text replace code works on some websites and breaks some other websites.

And people suggested that ob_end_flush() must be used with ob_start(), but the code fails if I include ob_end_flush in code after ob_start.

I like the solution 1, but is there any way to include headers and footers in it? Otherwise Solution 2 works for all websites, except that it does not have ob_end_flush and not recommended by many people, though a popular plugin uses it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Computer User
  • 2,839
  • 4
  • 47
  • 69

1 Answers1

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

}