Custom shortcode with attributes

Posted on 16th September, 2017 Leave a Comment

Building a custom shortcode is pretty easy.
Declare the vars and use ob_get_clean() to output the function.

<?php
function my_shortcode_func( $atts ) {
    extract( shortcode_atts( array(
    	'title' => '',
    	'desc' => '',
        'pdf' => '',
    ), $atts ) );

    ob_start();
?>



<p><?php echo $desc; ?></p>
<a class="button white" href="<?php echo $pdf; ?>" target="new"><?php echo $title; ?></a>


<?php
	$my_shortcode = ob_get_clean();
    return $my_shortcode;   
} 
add_shortcode( 'my_shortcode', 'my_shortcode_func' );

The shortcode should include the title, desc and url for the link.
[add_pdf_shortcode title=“the title” desc=“the description” pdf=“http://link.com”]

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]