Insert PHP Code Snippet Shortcode

Below, you’ll find a detailed guide on how to add the Insert PHP Code Snippet Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Insert PHP Code Snippet Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Insert PHP Code Snippet Plugin and the shortcodes it provides:

Plugin Icon
Insert PHP Code Snippet

"Insert PHP Code Snippet is a useful WordPress plugin that allows users to create and use PHP code snippets easily, simplifying complex coding tasks. The perfect solution for non-coders!"

★★★★☆ (631) Active Installs: 100000+ Tested with: 5.9.8 PHP Version: false
Included Shortcodes:
  • [xyz-ips]

Insert PHP Code Snippet [xyz-ips] Shortcode

The ‘xyz-ips’ shortcode from the Insert PHP Code Snippet plugin allows you to display content from a specified snippet. It queries the database for a snippet with the provided name and, if found, evaluates its content.

Shortcode: [xyz-ips]

Examples and Usage

Basic example – A shortcode for displaying content snippet by referencing its title.

[xyz-ips snippet="MySnippet"]

PHP Function Code

In case you have difficulties debugging what causing issues with [xyz-ips] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('xyz-ips','xyz_ips_display_content');

Shortcode PHP function:

function xyz_ips_display_content($xyz_snippet_name){
    global $wpdb;
    if(is_array($xyz_snippet_name)&& isset($xyz_snippet_name['snippet'])){
        $snippet_name = $xyz_snippet_name['snippet'];
        $query = $wpdb->get_results($wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."xyz_ips_short_code WHERE title=%s" ,$snippet_name));
        if(count($query)>0){
            foreach ($query as $sippetdetails){
                if($sippetdetails->status==1){
                  /*  if(is_numeric(ini_get('output_buffering'))){
                        $tmp=ob_get_contents();
                        if(strlen($tmp)>0)
                          ob_clean();*/
                        ob_start();
                        $content_to_eval=$sippetdetails->content;

/***** to handle old codes : start *****/

if(get_option('xyz_ips_auto_insert')==1){
    $xyz_ips_content_start='<?php';
    $new_line="\r\n";
    $xyz_ips_content_end='?>';

    if (stripos($content_to_eval, '<?php') !== false)
        $tag_start_position=stripos($content_to_eval,'<?php');
    else
        $tag_start_position="-1";

    if (stripos($content_to_eval, '?>') !== false)
        $tag_end_position=stripos($content_to_eval,'?>');
    else
        $tag_end_position="-1";

    if(stripos($content_to_eval, '<?php') === false && stripos($content_to_eval, '?>') === false)
    {
        $content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval;
    }
    else if(stripos($content_to_eval, '<?php') !== false)
    {
        if($tag_start_position>=0 && $tag_end_position>=0 && $tag_start_position>$tag_end_position)
        {
            $content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval;
        }
    }
    else if(stripos($content_to_eval, '<?php') === false)
    {
        if (stripos($content_to_eval, '?>') !== false)
        {
            $content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval;
        }
    }
    $content_to_eval='?>'.$content_to_eval;
}

/***** to handle old codes : end *****/
else{
    if(substr(trim($content_to_eval), 0,5)=='<?php')
        $content_to_eval='?>'.$content_to_eval;
}


$exception_occur=0;
$exception_msg="";

if(get_option('xyz_ips_auto_exception')==1) {

  $str1 = " try{";
  $var="$";
  $str2 = "}catch(Exception ".$var."e) {echo 'Caught exception: '.".$var."e->getMessage();".$var."exception_occur=1;".$var."exception_msg=".$var."e->getMessage();}";

  $first_start_tag=strpos($content_to_eval,"<?php");
  $last_end_tag=strrpos($content_to_eval,"?>");
  $last_start_tag=strrpos($content_to_eval,"<?php");

  if($first_start_tag>=0) {
    $new_pos=$first_start_tag+5; //add length of php start tag
    $content_to_eval=substr_replace( $content_to_eval, $str1, $new_pos, 0 );
  }
  if($last_end_tag<$last_start_tag) {
    $content_to_eval=$content_to_eval.$str2;
  }
  else {
    if($last_end_tag>0) {
      $new_pos=$last_end_tag+5; //add length of $str1
      $content_to_eval=substr_replace( $content_to_eval, $str2, $new_pos, 0 );
    }
    else if($last_end_tag==0) {
      $content_to_eval=$content_to_eval.$str2;
    }
  }
  //echo $content_to_eval;die;
}


                        eval($content_to_eval);

                        if($exception_occur==1) {

            							global $post;
            	            $post_slug = $post->post_name;

            							if($post_slug!="xyz-ics-preview-page") {

            								if(get_option('xyz_ips_exception_email')!="0" && get_option('xyz_ips_exception_email')!="") {

            									$email=get_option('xyz_ips_exception_email');
            									$headers= "Content-type: text/html";
            									$subject="Exception Report";
            									$message="Hi,<br>An exception occured while running one of the snippet.The snippet name is ".$snippet_name;
            									$message.=".<br>Exception details are given below<br>";
            									$message.=$exception_msg;
            									wp_mail($email, $subject, $message,$headers);
            								}
            							}
            						}

                        $xyz_em_content = ob_get_contents();
                       // ob_clean();
                        ob_end_clean();
                         return $xyz_em_content;
                  /*  }
                    else{
                        eval($sippetdetails->content);
                    }*/
                }
                else{
                    return '';
                }
                break;
            }
        }
        else{
            return '';
        }
    }
}

Code file location:

insert-php-code-snippet/insert-php-code-snippet/shortcode-handler.php

Conclusion

Now that you’ve learned how to embed the Insert PHP Code Snippet Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *