How to save the content of shortcode in wordpress options?

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?

Read More

I think, maybe I can do it with javascript, but I didn’t found it on the web.

Related posts

Leave a Reply

1 comment

  1. 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 your shortcode in its value attribute.

    You can then refer to that post meta data which holds your shortcode by using the get_post_meta(); method provided by WordPress.

    Here is the link to get_post_meta http://codex.wordpress.org/Function_Reference/get_post_meta

    You 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:

    echo '<input type="hidden" id="My-Shortcode" name="My-Shortcode" ' . get_post_meta($post->ID, 'My-Shortcode', true) . '/>';
    

    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 stating

    get_post_meta($post->ID, 'My-Shortcode', true)
    

    True 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:

    function My_callback_function( $post ){
        // your input will be here
    }
    

    In the front end you can then simply call get_post_meta again like:

    echo get_post_meta($post->ID, 'My-Shortcode', true)
    

    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