You should use this plugin to insert PHP code in a safe way!There are some way you can embed another PHP code in your website: by adding code snippet in functions.php, by making a WordPress plugin or using a plugin which allows you to make code snippets.

In this tutorial I will introduce you Woody ad snippets (aka Insert PHP) and why I chose this

Using Insert PHP

Insert PHP - 6 types of snippet

Woody ad snippets allows you to insert 6 types of snippet, in which:

  • PHP snippet = Used for inserting PHP code. Can be used for registering functions, hooks, global variables, printing text. Virtual functions.php.
  • Text snippet = Used for inserting formatted text. Can be used for inserting quotes, paragraphs, shortcodes from other plugins, tables, media files.
  • Universal snippet = Used for inserting PHP, HTML, JS& CSS code. Can be used for inserting ads, analytics, embeds & other complex scenarios.
  • CSS snippet = Used for inserting CSS. Can be used for inserting CSS styles.
  • JavaScript snippet = Used for inserting JS code. Can be used for inserting ads, analytics, embeds & other complex scenarios.
  • HTML snippet = Used for inserting HTML code. Can be used for inserting HMTL code.

In order to use PHP snippet, just make one, put your PHP code snippet there. For example. this is a code snippet I used to display my upcoming post (Post I will write but don’t have time for now…)

<?php
// the query
$all_posts = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'draft', 'posts_per_page' => -1 ) );

if ( $all_posts->have_posts() ) :
?>

  <ul>
    <?php while ( $all_posts->have_posts() ) : $all_posts->the_post(); ?>
      <li><i class="fa fa-envelope-o" aria-hidden="true"></i>
<?php the_title(); ?></a></li>
    <?php endwhile; ?>
  </ul>

<?php else : ?>
  <p><?php _e( 'Sorry, no posts were found.' ); ?></p>
<?php endif; ?>

<?php wp_reset_postdata(); ?>

To execute code snippet, you have 2 choices:

Insert PHP - Excute code

You can select Run Everywhere, the code will be executed as page load everywhere. Select Where There is a Shortcode will run code snippet where you embed the shortcode only.

For this specific purpose to display a Coming Soon posts, I put shortcode in a Widget as in picture below

Insert PHP - Put shortcode in widget

This is the result

Insert PHP - Coming Soon Posts

Why I chose Insert PHP

There are some reasons why I chose Insert PHP over some others of its kind:

  • You can make 6 different types of snippet.
  • You can use variable, attribute in snippet
  • You can safely return your site when something went wrong.

Insert PHP - Fix wrong PHP

Even an experienced person can make mistakes, especially when we are playing with PHP. Insert PHP provides a way to rollback to our site as following steps:

  1. Go to the safe mode by this link: http://your-site-name.dev/wp-admin/?wbcr-php-snippets-safe-mode
  2. Edit the snippet in which you made a mistake (or delete it)
  3. Leave safe mode by clicking the link: http://your-site-name.dev/wp-admin/?wbcr-php-snippets-disable-safe-mode

Great, now you should not have any issues!

GiangLe.DEV

My name is Giang, a Vietnamese guy living in Thanh Hoa City, Vietnam. I am a WordPress Developer, learning to master WordPress and Laravel PHP Framework.

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu