Here is the context : with a custom plugin, I create a shortcode which must retrieve a list of articles as text files (html inside h2, p, links) from a thematic folder. The html content of the text file must go to wp_postmeta> meta_value.

add_shortcode('myshortcode', 'my_shortcode');
function my_shortcode( $atts ){
  global $post;
  $attributes = shortcode_atts( array(
    'thematic' => '',
  ), $atts );

  $themaCurrent = $attributes['thematic'];
  $mypluginpath = WP_PLUGIN_DIR . '/myplugin/';

  $passage = fopen($mypluginpath.'nbr-init.txt', 'r+');//Stock passage, Start to 0
  $number_passage = fgets($passage);  // Read the first line
  $number_passage += 1; // +1
  fseek($passage, 0);// cursor at start of file
  fputs($passage, $number_passage); // Rewrite the new number

  $folderCurrent = $themaCurrent.'/';

  // wp_postmeta > meta_value stay empty :
  $filename = $themaCurrent.'-'.$number_passage.'.txt'; //like : thema-1.txt...
  //$filename = "static".".txt";// fill correctly in wp_postmeta > meta_value
  $mypath = $mypluginpath.$folderCurrent.$filename; 

  $meta_value = file_get_contents($mypath);
  $request = wp_remote_get($mypath);
  $meta_value = wp_remote_retrieve_body($request) ;
$meta_key = '_my_meta_key_name';
update_post_meta( $post->ID, $meta_key, $meta_value/*, true*/ );

return $meta_value;}

the problem : using file_get_contents :

  • by testing with a simple call to the "static" file : $filename = "static".".txt"; Result: writing html in meta_value is correct. Good point...

  • But with the dynamic call on $filename = $themaCurrent.'-'.$number_passage.'.txt'; Result: meta_value systematically remains empty.

Following advice here, I also tried to : replacing file_get_contents by $request = wp_remote_get($mypath); $meta_value = wp_remote_retrieve_body($request); Result: empty meta_value.

Also tried to indicate : update_post_meta( (int) $post->ID, $meta_key, (string) $meta_value);

and also with serialization like: $meta_value = serialize($meta_value);

Server-side details: php version 7.3 and allow_url_fopen is enabled.

I have scoured the site and tried several of the solutions given but after several attempts, I ask for your help, because I can not advance more. What am i doing wrong, how can i fix the problem please? many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.