WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type bhour, with a field bh_shortcode. When a post of this type is saved, I'd like it to add a shortcode based on the post's value of bh_shortcode.

If the value of bh_shortcode is "test", and when a [test] shortcode tag appears on a normal post type, nothing happens--[test] text is not replaced.

If I place add_shortcode('test','save_bhour_details'); outside of the function, the [test] text is replaced.

How can I use the add_shortcode function inside of another function?

function bhour_shortcode_handler(){
    global $post,$bhourdays;

    $output='<p>this is a test</p>';

    return $output; 

add_action('save_post', 'save_bhour_details');

function save_bhour_details(){
    global $post, $bhourdays;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){

    if ( get_post_type($post) != 'bhours'){

    //retrieve current values
    $current = get_post_custom();

        //remove current shortcode

        //save new value

        //get current values
        $current = get_post_custom();

        //register new shortcode
        add_shortcode($current['bh_shortcode'][0], 'bhour_shortcode_handler');

    //register new shortcode
    add_shortcode($current['bh_shortcode'][0], 'bhour_shortcode_handler');
share|improve this question
up vote 1 down vote accepted

Just use as per the following code sample in your plugin.,

function wp_shortcode( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    //return "foo = {$foo}";
    return your_function();
add_shortcode( 'your-shortcode', 'wp_shortcode' );

I think this may help for you to get your needs.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.