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 |1 Answer
Reset to default 0Since 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
[shortcode_counter] [shortcode_counter] [shortcode_counter]
on the admin panel, the first time I have 3 elements in theshortcode_keys
and when I hit refresh, 3 more elements are added upon every refresh. – chunkybyte Commented Apr 16, 2019 at 10:01