post meta - update_metadata() appends shortcode data for every page refresh

New to Wordpress.I have an implementation where I am writing multiple shortcodes on the admin panel and for every shortc

New to Wordpress.

I have an implementation where I am writing multiple shortcodes on the admin panel and for every shortcode, I will update a post metadata key with every shortcode I find on the post.

This is the code to update the metadata inside my shortcode :

<?php
    function shortcode_counter($atts)
    {
        $a = shortcode_atts(array(
            'attr1' => null
        ), $atts);


        $postID = get_the_ID();

        // 'shortcode_keys' is the metadata key
        if (metadata_exists('post', $postID, 'shortcode_keys')) {
            $shortcode_keys[0] = get_post_meta($postID, 'shortcode_keys');
        } else {
            $shortcode_keys = array();
        }
        $new_key = 'shortcode_widget_ids_' . sizeof($shortcode_keys);
        array_push($shortcode_keys, $new_key);
        update_metadata('post', $postID, 'shortcode_keys', $shortcode_keys, '');

        return '';
    }

    add_shortcode('shortcode_counter', 'shortcode_counter');
?>

Expected : 1. Add 3 shortcodes on post. 2. Add information about 3 shortcodes using update_metadata(). So the metakey has array of length 3.

Actual : 1. Upon first refresh, the metakey shows array of length 3 as expected. 2. Hit refresh again, and the metakey holds a long complex array.

My guess is the metadata holds information even after refresh. I need metadata because I want to use this value elsewhere on the code. Is there some other way in wordpress to hold this array information with gets only data of the 'n' shortcodes I add to the post.

New to Wordpress.

I have an implementation where I am writing multiple shortcodes on the admin panel and for every shortcode, I will update a post metadata key with every shortcode I find on the post.

This is the code to update the metadata inside my shortcode :

<?php
    function shortcode_counter($atts)
    {
        $a = shortcode_atts(array(
            'attr1' => null
        ), $atts);


        $postID = get_the_ID();

        // 'shortcode_keys' is the metadata key
        if (metadata_exists('post', $postID, 'shortcode_keys')) {
            $shortcode_keys[0] = get_post_meta($postID, 'shortcode_keys');
        } else {
            $shortcode_keys = array();
        }
        $new_key = 'shortcode_widget_ids_' . sizeof($shortcode_keys);
        array_push($shortcode_keys, $new_key);
        update_metadata('post', $postID, 'shortcode_keys', $shortcode_keys, '');

        return '';
    }

    add_shortcode('shortcode_counter', 'shortcode_counter');
?>

Expected : 1. Add 3 shortcodes on post. 2. Add information about 3 shortcodes using update_metadata(). So the metakey has array of length 3.

Actual : 1. Upon first refresh, the metakey shows array of length 3 as expected. 2. Hit refresh again, and the metakey holds a long complex array.

My guess is the metadata holds information even after refresh. I need metadata because I want to use this value elsewhere on the code. Is there some other way in wordpress to hold this array information with gets only data of the 'n' shortcodes I add to the post.

Share Improve this question edited Apr 16, 2019 at 9:57 chunkybyte asked Apr 16, 2019 at 7:46 chunkybytechunkybyte 12 bronze badges 2
  • Is that the entirety of your short code function? I'm having trouble following the logic. For instance, on every short code run except the first, where is $shortcode_keys defined? As written that's a local variable and falls out of scope when the function ends, so it's not there on the second short code. – tmdesigned Commented Apr 16, 2019 at 9:10
  • I have updated the entire code of the shortcode. So the thing is the post metadata holds the information even upon refreshing the page. I want to populate the metadata to be overwritten on every page refresh. So if I put [shortcode_counter] [shortcode_counter] [shortcode_counter] on the admin panel, the first time I have 3 elements in the shortcode_keys and when I hit refresh, 3 more elements are added upon every refresh. – chunkybyte Commented Apr 16, 2019 at 10:01
Add a comment  | 

1 Answer 1

Reset to default 0

Since my post meta data was holding this data even upon refresh, all I had to do was delete this metakey once all the shortcodes on my post content were processed.

For that, I made another shortcode and invoked that with do_shortcode() on single.php since that only processes once at the end. Now once I refresh the post, I update the post metadata with new information everytime.

This might not be the best solution. But if someone was looking for one, there you go.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577504a4634056.html

相关推荐

  • post meta - update_metadata() appends shortcode data for every page refresh

    New to Wordpress.I have an implementation where I am writing multiple shortcodes on the admin panel and for every shortc

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信