OpenHook Shortcodes

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

Before starting, here is an overview of the OpenHook Plugin and the shortcodes it provides:

Plugin Icon
OpenHook

"OpenHook is a versatile plugin for WordPress that simplifies customization of your Thesis theme. With its easy-to-use interface, you can effortlessly add HTML, CSS, or PHP code, enhancing your website's functionality."

★★★☆✩ (6) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [email]
  • [global]
  • [snap]

OpenHook [email] Shortcode

The Thesis-OpenHook plugin shortcode ’email’ is designed to protect email addresses from spam bots. This shortcode takes the ’email’ attribute and runs it through the ‘antispambot’ function in PHP. The function obfuscates the email address, making it unreadable to spam bots, while still visible and functional for users.

Shortcode: [email]

Examples and Usage

Basic example – The shortcode can be utilized to obfuscate an email address from spambots.

[email]example@example.com[/email]

Advanced examples

Using the shortcode to display multiple email addresses, separated by a comma. This can be useful for displaying a list of contact emails on a page.

[email]example1@example.com, example2@example.com, example3@example.com[/email]

Using the shortcode with additional text. The email address will be obfuscated, but the additional text will be displayed normally. This can be useful for providing context or additional information with the email address.

[email]For inquiries, contact us at: example@example.com[/email]

PHP Function Code

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

Shortcode line:

add_shortcode( 'email', array( $shortcodes, 'email' ) );

Shortcode PHP function:

function email( $atts , $content = null ) {
		return antispambot( $content );
	}

Code file location:

thesis-openhook/thesis-openhook/index.php

OpenHook [global] Shortcode

The Thesis-OpenHook shortcode ‘global’ retrieves specific global values. It extracts the desired key from the attributes and identifies the source of global values. If both a key and source page are set, it attempts to pull a global value. If not, it returns nothing.

Shortcode: [global]

Parameters

Here is a list of all possible global shortcode parameters and attributes:

  • key – The specific global value you want to retrieve
  • global_source – The source page for your global values

Examples and Usage

Basic example – Utilizing the shortcode to display a specific global value from a source page by referencing the key.

[global key="my_key" /]

Advanced examples

Employing the shortcode to display a specific global value from a source page by referencing the key. The shortcode will first attempt to retrieve the value by key, but if not found, it will return nothing.

[global key="my_key" global_source="my_source_page" /]

Using the shortcode without specifying a key. In this instance, the shortcode will return nothing as both a key and source page need to be set.

[global /]

Incorporating the shortcode with an incorrect source page. The shortcode will not be able to retrieve the global value and will return nothing.

[global key="my_key" global_source="incorrect_source_page" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'global', array( $shortcodes, 'globals' ) );

Shortcode PHP function:

function globals($atts) {
		# Get the desired key
		extract( shortcode_atts( array( 'key' => false ), $atts ) );

		# Determine the source of our global values
		$options = get_option( 'openhook_shortcodes' );
		$source = ( isset( $options[ 'global_source' ] ) && $options[ 'global_source' ] ) ? $options[ 'global_source' ] : false;

		# Only attempt to pull a global if both a key & source page are set
		if ( (string) $key && $source ) {
			return get_post_meta( $source, $key, true );
		} else {
			return;
		}
	}

Code file location:

thesis-openhook/thesis-openhook/index.php

OpenHook
 Shortcode</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The Thesis-OpenHook plugin shortcode enables PHP code execution within posts. It checks if the post author has the authority level and executes the PHP code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Shortcode</strong>: <code></code></p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"5px"} -->
<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Examples and Usage</h3>
<!-- /wp:heading -->

<p><strong>Basic example</strong> - The shortcode below uses the 'php' shortcode to run a PHP code snippet. The PHP code is passed as the content of the shortcode.</p>

<!-- wp:code -->
<pre class="wp-block-code"><code lang="php" class="language-php"> echo 'Hello, world!'; 

Advanced examples

Example 1 – The PHP shortcode can be used to run more complex PHP code. In the example below, the PHP code calculates the sum of two numbers.

 $a = 5; $b = 10; echo $a + $b; 

Example 2 – The PHP shortcode can also be used to run PHP functions. In the example below, the PHP code uses the ‘date’ function to print the current date.

 echo date('Y-m-d'); 

Example 3 – The PHP shortcode can also be used to run PHP code that interacts with WordPress. In the example below, the PHP code uses the ‘get_the_title’ function to print the title of the current post.

 echo get_the_title(); 

Please note that the ‘php’ shortcode should only be used by users who have the necessary permissions to run PHP code on the website, as it can pose a security risk if misused.

PHP Function Code

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

Shortcode line:

add_shortcode( 'php', array( $shortcodes, 'php' ) );

Shortcode PHP function:

function php( $atts, $content = null ) {
		global $openhook;

		# Prevent access to the shortcode via Ajax
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
			return;
		}

		# Only process this shortcode if the author of the post has the authority
		if ( author_can( get_the_ID(), $openhook->get_auth_level() ) ) {
			# Buffer the output of the PHP as we don't want to echo anything here
			ob_start();

			eval( "?>$content<?php " );
		
			return ob_get_clean();
		} else {
			return;
		}
	}

Code file location:

thesis-openhook/thesis-openhook/index.php

OpenHook [snap] Shortcode

The Thesis-OpenHook shortcode ‘snap’ is designed to create website snapshots. It takes a URL and generates an image preview of the site. The shortcode has default values like the URL to be snapshotted, alt text for the image, width, height, and CSS classes. These can be customized as per requirements. The shortcode sanitizes the data and outputs an IMG tag with the snapshot. This can be a handy tool for creating visual previews of websites within your content.

Shortcode: [snap]

Parameters

Here is a list of all possible snap shortcode parameters and attributes:

  • url – web address of the site to be captured
  • alt – alternative text for the snapshot image
  • w – width of the website snapshot
  • h – height of the website snapshot
  • class – CSS classes for the snapshot image

Examples and Usage

Basic example – A simple usage of the ‘snap’ shortcode to generate a snapshot of a webpage with default parameters.

[snap /]

Advanced examples

Using the ‘snap’ shortcode with a custom URL. This will generate a snapshot of the specified webpage.

[snap url="https://www.customwebsite.com/" /]

Using the ‘snap’ shortcode with custom URL and dimensions. This will generate a snapshot of the specified webpage with the defined width and height.

[snap url="https://www.customwebsite.com/" w=500 h=400 /]

Using the ‘snap’ shortcode with all available parameters. This will generate a snapshot of the specified webpage with the defined width, height, alt text and CSS class.

[snap url="https://www.customwebsite.com/" w=500 h=400 alt="Custom Snapshot" class="custom_class" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'snap', array( $shortcodes, 'snap' ) );

Shortcode PHP function:

function snap( $atts, $content = null ) {
		# Default values
		$defaults = [
			'url' => 'https://www.example.com/', # URL to be snapshotted
			'alt' => __( 'Website Snapshot', 'thesis-openhook' ), # Alt text for snapshot image
			'w' => 400, # Width of snapshot
			'h' => 300, # Height of snapshot
			'class' => '', # CSS class(es), space separated
		];

		# Parse attributes
		$atts = shortcode_atts( $defaults, $atts, 'snap' ); # @filter: shortcode_atts_snap

		# Sanity checks to ensure proper variables
		$url = urlencode( wp_http_validate_url( $atts['url'] ) ?: $defaults['url'] );
		$alt = esc_attr( $atts['alt'] );
		$w = absint( $atts['w'] ) ?: $defaults['w'];
		$h = absint( $atts['h'] ) ?: $defaults['h'];
		$class = ! empty( $atts['class'] ) ? esc_attr( $atts['class'] ) . ' website_snapshot' : 'website_snapshot';

		# Put together our IMG tag to be output, with final data sanitation
		$img = '<img src="https://s.wordpress.com/mshots/v1/' . $url . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '" class="' . $class . '">';

		return $img;
	}

Code file location:

thesis-openhook/thesis-openhook/index.php

Conclusion

Now that you’ve learned how to embed the OpenHook Plugin shortcodes, 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 *