0

my simple shortcode retrieves a list of articles as text files from a folder. These articles are in html format (with h2, p ...). I use a plugin which creates the pages I need on the fly in which I insert my shortcode. The shortcode works well and the html rendering is ok in the articles, BUT:

the problem: the shortcode updates with each page load and makes different content each time.

What do you think is the method to "fix" this because I can't? I tried to write the content of the shortcode in the database in post_content? And remove the shortcode from the articles to be able to edit them later in wordpress. I have made several attempts but nothing, thank you for your help

function my_shortcode(){

    $path_to_myplugin = WP_PLUGIN_DIR . '/my_plugin_name/';
    $thema_folder = "themafolder";
    $passage = fopen($path_to_myplugin.'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
    fclose($passage);
    @$file_name = $thema_folder."/"."art-".$thema_folder."-".$number_passage.".txt";
    $content = file_get_contents($path_to_myplugin.$file_name);// Ok it works 

// I try to write the shortcode to the post_content but not working:
    global $wpdb;
    $wpdb->insert{
        $args = array(
            'post_content' => $content
        )
    };
$content = wp_insert_post( $args );

    return $content;
}

add_shortcode ('myshortcode', 'my_shortcode');

1 Answer 1

0

The code you have currently is going to create a new post rather than update the post where the shortcode is in use. In order to update the post, you'll need to include its ID (see the documentation).

Here's how you can update your code to include the ID:

global $post; // Use the global $post object for the current post.
$args = array(
    'ID'           => $post->ID,
    'post_content' => $content,
);
wp_insert_post( $args ); // Update the current post with the $content.

return $content;

I also removed the $wpdb usage because you don't need to access the database directly; the wp_insert_post function will take care of it for you.


Note: I've focused this answer on just the part of the code that I think is failing you. I'm not convinced the shortcode approach is an ideal solution (for instance, it might be better to write a simple plugin that loops through your files and creates all the post entries one time). However, I hope this answer helps you achieve your goal right now; there's always the opportunity to optimize as needed.

3
  • thanks for response and all informations! It works, but partially. It's strange, some articles render html as well and therefore remove the shortcode, but not for all. I noticed that by reloading the page several times or by clearing the cache, in some cases the html ends up being written in the base. Any idea?
    – Codepix
    Commented May 15, 2020 at 0:39
  • If you've got a caching plugin enabled (or your host caches), it's likely going to be a challenge to track down the issue. You may need to resort to a more active approach to populate the database if it's not practical to wait until it eventually happens. Commented May 15, 2020 at 17:33
  • I understand, Thank you. I'm not using a cache plugin. Your solution work perfectly without the third party plugin i use to generate. With the generation plugin, my shortcode replace his content instead of add the content (a part generate by the plugin) > but i think it's another problem. So many thanks for your help, it help me a lot.
    – Codepix
    Commented May 22, 2020 at 12:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.