1. Introduction #

1.1.What is a snippet? #

Snippet is a programming term for a small region of re-usable source code, machine code, or text. Ordinarily, these are formally defined operative units to incorporate into larger programming modules. Snippet management is a feature of some text editors, program source code editors, IDEs, and related software. It allows the user to avoid repetitive typing in the course of routine edit operations.

1.2.Purpose of the plugin #

PostSnippets allows you to build a library with snippets of HTML, PHP code, or reoccurring text that you often use in your posts and pages.

You can use predefined variables to replace parts of the snippet. All snippets are available in the post editor via a button in the Visual mode. You can insert the snippet directly or as a shortcode to keep flexibility for updating the snippet. PHP code is also supported in PostSnippets and can be inserted as shortcodes.

2.Plugin Configuration #

2.1.How to add a title? #

Identify snippets in the post editor by giving it a title. This also becomes the name of the short-code if you enable that option.

2.2.How to add a description? #

You can add an optional description for the Snippet. If filled out, the description will be displayed in the Snippet’s insert window of the post editor.

2.3.How to add variables? #

You can reference a comma-separated list of custom variables in your snippet. A variable can also be assigned a default value by using the equal sign, variable=default.

Example
url,name,role=user,title

2.4.How to enable snippet? #

This is the block of text, HTML, or PHP that can either be inserted in the post directly or as a short-code. If you have entered predefined variables, you can reference them from the snippet by enclosing them in {} brackets.

Example
To reference the variables in the example above, you would enter {url} and {name}. So if you enter this snippet:

So if you enter this snippet:

This is the website of <a href=”{url}”>{name}</a>

You will get the option to replace URL and name on insert if they are defined as variables.

2.5.How to enable shortcode? #

When enabling the short-code checkbox, the snippet is no longer inserted directly but instead inserted as a short-code. The apparent advantage of this is that you can insert a block of text or code in many places on the site and update the content from one place.

The name to use the short-code is the same as the snippet title (spaces are not allowed). When inserting a short-code snippet, the short-code and not the content will be inserted in the post.

If you enclose the short-code in your posts, you can access the enclosed content by using the variable {content} in your snippet. The {content} variable is reserved, so don’t use it in the variables field.

2.6.How to enable PHP? #

Snippets defined as shortcodes can optionally also be evaluated as PHP Code by enabling the PHP checkbox. PHP snippets are only available when treating the snippet as a shortcode.

Check this image for an example PHP snippet:

 

With a snippet defined like the one above, you can call it with its shortcode definition in a post. Let’s pretend that the example snippet is named PHP code and have one variable defined loop_me; then it would be called like this from a post:

[phpcode loop_me=”post snippet with PHP!”]


When the shortcode is executed, the loop variable will be replaced with the string supplied in the shortcode, and then the PHP code will be evaluated. (Outputting the string five times in this case. Wow!)

Also note that a PHP snippet doesn’t need to be wrapped in <?php #code; ?>.

2.7.How to render snippet on page using the Gutenberg editor #

With your snippets set up, you’ll find a new button in your post editor (circled in the image above), which you can click to open the Post Snippet window to insert one of your snippets in your post.

When you click this button, you get a window with all your available snippets.

Each snippet has its own tab in the Post Snippet window, so you can select which snippet to insert. If you have set up custom variables for the snippet, you can now enter the values for these variables before clicking insert, and they will be inserted in the correct places in the snippet string.

2.8.wptexturize #

Before the shortcode is outputted, it can optionally be formatted with wptexturize to transform quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol.

3.Advanced (for developers) #

You can add constants to wp-config.php or the theme’s functions.php file to control some aspects of the plugin. Available constants to set are:

 

// Allow users with edit_posts capability access to the Post Snippets admin.

define(‘POST_SNIPPETS_ALLOW_EDIT_POSTS’, true);


// Disable PHP Execution in snippets and removes the options from admin.

define(‘POST_SNIPPETS_DISABLE_PHP’, true);

You can retrieve a Post Snippet directly from PHP, in a theme, for instance, by using the PostSnippets::getSnippet() method.

Usage

<?php $my_snippet = PostSnippets::getSnippet( $snippet_name, $snippet_vars ); ?>

Parameters

$snippet_name

(string) (required) The name of the snippet to retrieve.

 

$snippet_vars

(string) The variables to pass to the snippet, formatted as a query string.

Example

// Use query string for variables

$mySnippet = PostSnippets::getSnippet(‘internal-link’, ‘title=Awesome&url=2011/02/awesome/’);

echo $mySnippet;

// Use array for variables

$mySnippet = PostSnippets::getSnippet(‘internal-link’, array(‘title’ => ‘Awesome’, ‘url’ => ‘2011/02/awesome/’);

echo $mySnippet;

3.1.Filters #

The following filters are available for hooking into the plugin:

 

  • post_snippets_import   serialized array   Modify snippets and related data before the imported file populates the snippets.
  • post_snippets_export   serialized array   Modify snippets and related data before the export file is created.
  • post_snippets_snippets_list   array   Modify the array of snippets that are used as the snippet list for the jQuery UI dialog in the edit post screen.

 

Examples

 

Post_snippets_export

// Filter Exported Snippets

function ps_export( $snippets )

{

  $snippets = unserialize( $snippets );

  foreach ( $snippets as &$snippet ) {

    // Do something here. Example below search and replaces in snippets

    $snippet[‘snippet’] = str_replace(‘search’,’replace’, $snippet[‘snippet’]);

  }

  return serialize( $snippets );

}

add_filter( ‘post_snippets_export’, ‘ps_export’ );

 

Post_snippets_import

 

// Filter Imported Snippets

function ps_import( $snippets )

{

  $snippets = unserialize( $snippets );

  foreach ( $snippets as &$snippet ) {

    // Do something here. Example below search and replaces in variables

    $snippet[‘vars’] = str_replace(‘search’, ‘replace’, $snippet[‘vars’]);

  }

  return serialize( $snippets );

}

add_filter( ‘post_snippets_import’, ‘ps_import’ );

3.2.Rest API #

Introduction:

Post Snippets REST API endpoints allow you to create, update, delete and get the snippet.

3.3.Post Snippets (REST API) #

  • Navigate to the WordPress Admin Dashboard > Post Snippets
  • Click on the REST API menu.
  • Inside the Post Snippet REST Settings, you can find the following options:
    • Post Snippets Enable
    • Post Snippets Token
    • Gell all snippets
    • Get a particular snippet.
    • Delete a particular snippet
    • Delete multiple snippets
    • Update snippet
    • Create snippet

3.4.How to enable Post Snippets #

  • Click on the checkbox to enable the post snippets.

3.5.How to generate a Post Snippets Token #

  • Click on the Generate Token button.
  • You can find generated token(xxxxxxxxxxxxxxxx) code from the input field.
  • Each time you click on the generate token button, it will create a unique token.

3.6.How to create a snippet #

  • Enter the snippet title
  • Then click the Save Changes button.

Note: Do not enter any special characters [!@#$%^&*_(=+-)] into the title.

  • Right below the snippet title field, you can see the REST API URI.
  • That will look like this:
    • https://mywebsite.com/wp-json/post-snippets/my-snippet/{token}/{snippet-title}/{vars}/{shortcode}/{php}/{wptexturize}/snippet?snippet_code={snippet-code}&description={snippet-description}
  • Use the following URI parameters to create a snippet
    • {token} : 6fB9Ied3Tt8BbLyy
    • {snippet-title} : sample-snippet 
    • {vars}: use 0 for empty or enter comma separated values e.g: bob, rob, den
    • {shortcode}: 1 for enable or 0 for disable/empty 
    • {php}: 1 or 0
    • {wptexturize}: 1 or 0 
    • {snippet-code}: write simple/plain text, HTML, PHP etc.
    • {snippet-description}: lorem ipsum is simply dummy text

  • Response result should look like this:

  • Snippet successfully created

 

3.7.How to update a snippet #

  • To update a created snippet
  • Head over to the REST API settings page
  • Enter the Update Snippet endpoint and
  • Click on the Save Changes button

  • Use the following URI parameters to update a snippet
    • {token} : 6fB9Ied3Tt8BbLyy
    • {snippet-title} : sample-snippet 
    • {vars}: use 0 for empty or enter comma separated values e.g: name, email, dob
    • {shortcode}: 1 for enable or 0 for disable/empty 
    • {php}: 1 or 0
    • {wptexturize}: 1 or 0 
    • {snippet-code}: write simple/plain text, HTML, PHP etc.
    • {snippet-description}: PHP is a general-purpose scripting language especially suited to web development.

  • Response result:

  • Snippet successfully updated

 

3.8.How to get a particular snippet #

  • Enter endpoint title e.g: get-particular-snippet
  • Get the endpoint URI by clicking on the Save Changes button

  • Once the changes have been saved you will get the endpoint URI
  • Get a particular endpoint URI
  • https://mywebsite.com/wp-json/post-snippets/get-particular-snippet/{token}/{snippet-title}
  • Use the following parameters to get a particular snippet
  • {token} : 6fB9Ied3Tt8BbLyy
  • {snippet-title} : sample-snippet
  • Response result:

3.9.How to get all snippets #

  • Enter endpoint title e.g: get-all-snippets
  • Get the endpoint URI by clicking on the Save Changes button

    • Use a {token} parameter to get all snippets that you have created 
      • {token} : 6fB9Ied3Tt8BbLyy

  • Response result

3.10.How to delete a particular snippet #

  • Enter endpoint title e.g: del-snippet
  • Get the endpoint URI by clicking on the Save Changes button

  • Delete a particular snippet endpoint URI
  • Following parameters are required to delete a particular snippet
    • {token}: 6fB9Ied3Tt8BbLyy
    • {snippet-title}: Enter the snippet title that you want to delete. e.g: html-snippet

  • Response result:

3.11.How to delete multiple snippets #

  • Enter endpoint title e.g: del-multiple-snippets
  • Get the endpoint URI by clicking on the Save Changes button

  • Response result:

3.12.How to setup Gutenberg Block (Front-end) #

  • Go to Post/Pages
  • Add New or edit existing Post/Pages
  •  Click on the + icon and insert Post Snippets block

  • You can find the created snippet from the drop-down menu

  • Select any snippet 
  • Click on the Publish button

  • Click on the view page link
  • See the snippet result on the frontend

Help Guide Powered by Documentor
Suggest Edit
Scroll to Top