The PHP Shortcode Hunter, nested shortcodes and custom shortcodes

Overview

shortcode-hunter

The Shortcode Hunter - PHP shortcode class, nested shortcodes and custom shortcodes

Installation

composer require enesakarsu/shortcode-hunter

Syntax of shortcodes

Shortcodes are written in square brackets.Every opened shortcodes should be closed. Usage example;

[example]shortcode[/example] and example text

You can send parameters to the shortcodes if you want. Parameters are written in two curly braces. Usage example;

[post id={{22}}][/post]
and
[text color={{red}}]colored text[/text]

Why do shortcodes use?

You can capture shortcodes in a text and make them do whatever you want.

Example: [post id={{1}}][/post]

We can capture this shortcode in the text and pull the content with an id value of 1 from the database.

Example: [date][/date]

We can create a shortcode named "date" and print the current time in text.

What you (guys)can do is totally up to your imagination.

Usage Example

Creating a shortcode

To create a shortcode, open the shortcode-hunter/src/shortcodes.php file and create the shortcode you want like the example below. While creating the shortcode, we write the name of our shortcode as the first parameter and the name of the function we want to run as the second parameter.

Normal function:

$shortcode->create("example", "exampleFunction");

Class method:

$shortcode->create("example", "class@method");

Above, we asked it to run the function named "exampleFunction" for the "example" shortcode. Now let's create that function in the shortcode-hunter/src/functions.php file.


NOTE

The first parameter of the function to be created should be $values ​​and the second parameter should be $content. Otherwise, parameters and content cannot be accessed.


function exampleFunction($values, $content){
return "hello";
}

Parsing the shortcode

Create an index.php file and include the shortcode-hunter class in the file. Then send some text as a parameter to the shortcode's unparse method.

<?php
include "shortcode-hunter/src/hunter.php";

$text = "Some text and [example]shortcode[/example]";

echo $shortcode->parse($text);

?>

If I run the index.php file, the output I will get is:

Output: hello

Accessing the content of the shortcode

If I want to access the content of the shortcode;

function exampleFunction($values, $content){
return $content;
}

Output: shortcode

Getting parameters sent to shortcode

[example id={{22}}]shortcode[/example] If we are sending parameters to the shortcode, we can capture them like this;

function exampleFunction($values, $content){
return $values["id"];
}

Output: 22


NOTE

If you echo instead of return in the function that the shortcode will run, it will appear at the very beginning of the text. To avoid this you need to do output buffering.

ob_start();

//your codes
//your codes

$output = ob_get_contents();
ob_end_clean();

return $output;

Example;

function exampleFunction($values, $content){

ob_start();

$fruits = ["apple", "banana", "strawberry"];

foreach($fruits as $fruit){
echo $fruit;
}

$output = ob_get_contents();
ob_end_clean();

return $output;

}

You might also like...
WPGraphQL for Advanced Custom Fields
WPGraphQL for Advanced Custom Fields

WPGraphQL for Advanced Custom Fields WPGraphQL for Advanced Custom Fields automatically exposes your ACF fields to the WPGraphQL Schema. Install and A

Adds Settings to the Custom Post Type UI plugin to show Post Types in WPGraphQL

DEPRECATION NOTICE 🚀 Custom Post Type UI v1.9.0 introduced formal support for WPGraphQL!!! 👋 With that, this plugin is being deprecated and will no

Custom WordPress theme for DotOrg

wd_s Debt Collective Theme Table of Contents Introduction Getting Started Prerequisites Quick Start Advanced Setup Development Contributing and Suppor

Word Count (Custom WordPress Plugin)
Word Count (Custom WordPress Plugin)

word-count (Custom WordPress Plugin) Followed a tutorial to create a plugin that adds word count infos to the frontend (Posts only). I then modified t

A super simple abstraction to make creating a custom Taxonomies in WordPress a breeze

A super simple abstraction to make creating a custom Taxonomies in WordPress a breeze

🚀WordPress Plugin Boilerplate using modern web techs like TypeScript, SASS, and so on... on top of a local development environment with Docker and predefined GitLab CI for continous integration and deployment!
🚀WordPress Plugin Boilerplate using modern web techs like TypeScript, SASS, and so on... on top of a local development environment with Docker and predefined GitLab CI for continous integration and deployment!

WP React Starter: WordPress React Boilerplate DEPRECATED: WP React Starter was a "research project" of devowl.io for the development of our WordPress

This WP plugin will update GitHub, Bitbucket, GitLab, and Gitea hosted plugins and themes
This WP plugin will update GitHub, Bitbucket, GitLab, and Gitea hosted plugins and themes

Transition from GitHub Updater 9.x to Git Updater 10.x Due to the renaming of the plugin folders and files, after the initial update, the plugin will

A curated list of Awesome WordPress Theme, Plugins and Framework development Resources and WordPress Communities.

Awesome WordPress A curated list of Awesome WordPress Theme, Plugins and Framework development Resources and WordPress Communities. Inspired by bayand

A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings.
A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings.

Suspend WP A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings. 😎 Demo (coming so

Releases(v0.1)
Owner
Enes Akarsu
Enes Akarsu
Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer.

Description Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer. Support [audio] tag, compatible with AMP. Requirement WordPr

Karl Chen 24 Nov 3, 2022
Base classes for creating WordPress shortcodes.

WDS Shortcodes Contributors: WebDevStudios, jtsternberg, JayWood Donate link: http://webdevstudios.com Tags: shortcode button, shortcodes, cmb2, utili

WebDevStudios 26 Jun 19, 2020
A WordPress package for updating custom plugins and themes based on an API response from a custom update server.

WordPress Update Handler A WordPress package for updating custom plugins and themes based on an JSON REST API response from a custom update server. Ch

WP Forge 7 Oct 5, 2022
A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.

WP Bootstrap Navwalker This code in the main repo branch is undergoing a big shakeup to bring it in line with recent standards and to merge and test t

WP Bootstrap 3.3k Jan 5, 2023
Wordpress advance plugin with multi purposes features like live chat, custom post type, custom forms, word count etc

What is this? This is wordpress plugin which is created for the multi purpose uses. How to use? Simply install the plugin. Go to the plugin settigs pa

Amir Liaqat 2 Jun 23, 2022
Wordless is a junction between a WordPress plugin and a theme boilerplate that dramatically speeds up and enhances your custom theme creation

Wordless is a junction between a WordPress plugin and a theme boilerplate that dramatically speeds up and enhances your custom theme creation. Some of

weLaika 1.4k Dec 9, 2022
Create custom WordPress routes and redirects, restrict access by roles and/or capabilities. Routes made simple

Create custom WordPress routes and redirects, restrict access by roles and/or capabilities. Routes made simple

Joan 9 Oct 10, 2022
A custom update API for WordPress plugins and themes

A custom update API for WordPress plugins and themes. Intended to be used in conjunction with my plugin-update-checker library.

Yahnis Elsts 717 Dec 31, 2022
A PHP Class for creating Wordpress Custom Post Types easily

N.B I've released an updated version of the project to a new repository, PostTypes. WP Custom Post Type Class v1.4 A single class to help you build mo

Joe Grainger 412 Nov 25, 2022
Gutenberg Custom Fields... wait what?

Gutenberg Custom Fields Gutenberg Custom Fields allows you to control the content of the Gutenberg edit screen by creating pre-filled templates. Navig

Riad Benguella 192 Dec 24, 2022