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

plugins - add_action('the_content', 'my_plugin_content') is null

programmeradmin6浏览0评论

I am creating in personal plugin where I recover data from a personal table. Con add_action('the_content', 'my_plugin_content'); posso far vedere il contenuto in una pagina. I would like to save the content in wp_posts with add_filter ('save_post', 'my_plugin_content'), but the content is empty.

function my_plugin_content($content){
     $current_page = $wp->request;
     include_once(plugin_dir_path( __FILE__ ).'views/view.php');
     $obj = new Loader;
     $content.=$obj->controller($current_page);
     $my_post = array(); 
     $my_post['ID'] = $post->ID;
     $my_post['post_content']=$cont;
}

in another file I run the data recovery query that I refer to the file loader that loads the views

File loader

public function view($view, array $dati){
      require(DIR_PLUGIN.'views/'.$view.'_v.php');
}

File view

    <?php foreach($dati as $p){  echo '<p><strong>'.$p->NAME.'</strong></p>; }?>

I am creating in personal plugin where I recover data from a personal table. Con add_action('the_content', 'my_plugin_content'); posso far vedere il contenuto in una pagina. I would like to save the content in wp_posts with add_filter ('save_post', 'my_plugin_content'), but the content is empty.

function my_plugin_content($content){
     $current_page = $wp->request;
     include_once(plugin_dir_path( __FILE__ ).'views/view.php');
     $obj = new Loader;
     $content.=$obj->controller($current_page);
     $my_post = array(); 
     $my_post['ID'] = $post->ID;
     $my_post['post_content']=$cont;
}

in another file I run the data recovery query that I refer to the file loader that loads the views

File loader

public function view($view, array $dati){
      require(DIR_PLUGIN.'views/'.$view.'_v.php');
}

File view

    <?php foreach($dati as $p){  echo '<p><strong>'.$p->NAME.'</strong></p>; }?>
Share Improve this question edited Apr 1, 2020 at 18:32 Tom J Nowell 61.1k7 gold badges79 silver badges148 bronze badges asked Apr 1, 2020 at 18:11 LucaLuca 192 bronze badges 3
  • You need to indent your code, a decent code editor will do this automatically, otherwise your code becomes very difficult for other people to read :( Have you removed parts of your filter? – Tom J Nowell Commented Apr 1, 2020 at 18:31
  • I want to recover the contents of the view file with array variables php. The correct file View. public function view($view, array $dati){ return require(DIR_PLUGIN.'views/'.$view.'_v.php'); } – Luca Commented Apr 1, 2020 at 21:39
  • Ah so you want that as a string? That's a general PHP issue not a WP issue, use output buffers – Tom J Nowell Commented Apr 1, 2020 at 22:42
Add a comment  | 

2 Answers 2

Reset to default 0

Filters always return something, they're not actions, they take something in, modify it, then return it, but your filter does neither.

As a result, PHP declares that it returned null, and the next filter along recieves null as the post content.

For example, this filter appends the word "Hey!" to the end of the content:

add_filter( 'the_content', function ( $content ) {
    return $content . 'Hey!';
} );

So here are things you need to keep in mind:

  • filters get called a lot, do not do heavy work in filters
  • filters are for filtering, you take the first parameter, do things to it, then return it
  • filters always return something
  • do not echo stuff out on a filter, you're meant to return the full value with your modifications applied

The content was null because the return statement was missing in some files. I used the output buffers to retrieve the complete contentuno.. I decided to save the content in the main wordpress table.

function my_plugin_content($content){
     $current_page = $wp->request;
     include_once(plugin_dir_path( __FILE__ ).'views/view.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);
}

The problem is that the content is added to the content of the existing post. I want the content to be replaced. What is the best practice?

发布评论

评论列表(0)

  1. 暂无评论