最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Run plugins only on certain pages

programmeradmin4浏览0评论

I want to use my personal plugin only in some pages. If I remove the add_action function ('the_content', 'my_plugin_content'); the plugin does not show me the content of the other pages that are not in in_array ();

function my_plugin_content($content){
  global $post; global $wp; global $wpdb;
  $page =array('page1', 'page2','page3');
  $current_page = $wp->request;
  if(in_array($current_page, $page))
  {
   $old_content=$post->post_content;
   $sql = "UPDATE wp_posts SET post_content = '' WHERE ID = $post->ID";
   $wpdb->get_results($sql);
   include_once(plugin_dir_path( __FILE__ ).'loaders/loaders.php');
   $obj = new Loader;
   $content.=$obj->controller($current_page);
   $my_post = array(); 
   $my_post['ID'] = $post->ID;
   $my_post['post_content']=$content;
   wp_update_post($my_post);
 }
}
add_action('the_content', 'my_plugin_content');

I want to use my personal plugin only in some pages. If I remove the add_action function ('the_content', 'my_plugin_content'); the plugin does not show me the content of the other pages that are not in in_array ();

function my_plugin_content($content){
  global $post; global $wp; global $wpdb;
  $page =array('page1', 'page2','page3');
  $current_page = $wp->request;
  if(in_array($current_page, $page))
  {
   $old_content=$post->post_content;
   $sql = "UPDATE wp_posts SET post_content = '' WHERE ID = $post->ID";
   $wpdb->get_results($sql);
   include_once(plugin_dir_path( __FILE__ ).'loaders/loaders.php');
   $obj = new Loader;
   $content.=$obj->controller($current_page);
   $my_post = array(); 
   $my_post['ID'] = $post->ID;
   $my_post['post_content']=$content;
   wp_update_post($my_post);
 }
}
add_action('the_content', 'my_plugin_content');
Share Improve this question asked Apr 3, 2020 at 17:45 LucaLuca 192 bronze badges 5
  • What does your code do? If you only want to run this code on certain pages you'll need to identify what it does and how it will know which pages to run on. That filter will run in lots of different places, it's not just used for the_content(), it might even be used on excerpts, and it'll be used on RSS feeds etc – Tom J Nowell Commented Apr 3, 2020 at 18:02
  • I suspect you're trying to auto-update the contents of those 3 posts, perhaps from a 3rd party source or a local non-WP source, etc, if that's the case, there are far, far better ways to do this than messing with the_content. Keep in mind that the_content is a filter, it has to return a value, that filter will break content rendering across the entire site because it has no return – Tom J Nowell Commented Apr 3, 2020 at 18:18
  • I want to load content from another table into wp_posts and then show it as a normal page. What solution do you recommend. – Luca Commented Apr 3, 2020 at 19:11
  • Ah that's what shortcodes are for! – Tom J Nowell Commented Apr 3, 2020 at 19:13
  • 'the_content' is a filter hook, not an action hook. – Michael Commented Apr 4, 2020 at 1:46
Add a comment  | 

1 Answer 1

Reset to default 0

I want to load content from another table into wp_posts and then show it as a normal page. What solution do you recommend.

If that's your goal, then use shortcodes!

For example:

// PHP code
add_shortcode( 'footag', 'wpdocs_footag_func' );
function wpdocs_footag_func( $atts ) {
    return "foo = ".$atts['foo'];
}

Then in your content:

[footag foo="bar"]

This prints foo = bar on the frontend

The [footag] will get replaced by what your shortcode function returns, and it'll happen when the page is generated. You can pass attributes to it too

Read more about add_shortcode and check the examples at the official docs here:

https://developer.wordpress/reference/functions/add_shortcode/

发布评论

评论列表(0)

  1. 暂无评论