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
|
2 Answers
Reset to default 0Filters 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 toreturn
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?
public function view($view, array $dati){ return require(DIR_PLUGIN.'views/'.$view.'_v.php'); }
– Luca Commented Apr 1, 2020 at 21:39