how to create shortcode in wordpress

The question:

So I have the following code:

 <span class="delete"><a onclick="return confirm('Are you sure?');" href="<?php echo wp_nonce_url( add_query_arg( array( 'action' => 'my-delete-product', 'product_id' => $post->ID ), my_get_navigation_url('products') ), 'my-delete-product' ); ?>" rel="nofollow noreferrer noopener"><?php _e( '삭제', 'my' ); ?></a> | </span>

It is a simple button.

I want to make this into a shortcode so that I can use it anywhere where appropriate.

Any suggestions?

Thanks.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You can create your short code just few steps.

function short_codeFunction_name( $atts, $content=null ) {
    shortcode_atts( array(), $atts);
    $rowin = '<div class="row">'.do_shortcode( $content ) .'</div>';
    return $rowin;
}
add_shortcode( "your_shortcode_name", "short_codeFunction_name" );
  1. Then you can access [your_shortcode]Here your
    content[your_shortcode]
    format inside the Post,page etc.
  2. add this function inside function.php in active theme directory.

  3. use inside your theme directory

Method 2

The WordPress API documents this very thoroughly. Check it out here.

And if you are feeling lazy, here is a custom generator.

If you have a specific question beyond that, comment below.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment