admin管理员组文章数量:1432627
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.
本文标签: post metaupdatemetadata() appends shortcode data for every page refresh
版权声明:本文标题:post meta - update_metadata() appends shortcode data for every page refresh 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745577504a2664422.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