I am developing a plugin with WordPress. But I need to save a short code like [my-shortcode] and then in my front end show the final result of the short code. Imagine that I have one form where you can upload an image, select width, height and preloaded themes, animations, etc. But I want to use short code.
How to save the compiled short code with do_shortcode()
with WordPress options?
I think, maybe I can do it with javascript, but I didn’t found it on the web.
You cannot save a shortcode with
do_shortcode()
.do_shortcode()
is used to render a shortcode.You can use a
metabox
that renders an input and then you can use your JavaScript code that targets that input and stores yourshortcode
in itsvalue attribute
.You can then refer to that
post meta data
which holds your shortcode by using theget_post_meta();
method provided by WordPress.Here is the link to
get_post_meta
http://codex.wordpress.org/Function_Reference/get_post_metaYou can refer to how to add a metabox in WordPress here:
http://codex.wordpress.org/Function_Reference/add_meta_box
Your input tag should be coded like so in your callback function:
The input with the name
My-Shortcode
will actually hold your shortcode but as you see it requires a$post object
which actually hold all the data for that post. You can then refer to your shortcode key by statingTrue
simply means the value will be echoed out.Now in order to access any data within that
$post object
you need to pass the $post parameter to your function. Normally WordPress gives you access to that$post object
but you need to state that in your metabox callback function like so:In the front end you can then simply call get_post_meta again like:
Keep in mind that there are security issues when saving data to and from your WordPress database which is why you would like to use WordPress’s nonce system. Here’s the link. Investigate to know more: http://codex.wordpress.org/WordPress_Nonces