Skip to content

Tutorial PHP_Function Shortcode

EkoJr edited this page Jul 6, 2017 · 2 revisions

Summary

The [php_function] shortcode is a method to create custom shortcodes to be used within the List Content. It enables the the ability to create custom shortcodes for custom formatting, additional data, & other functions.

Important Note: One param is required with the custom php function. By default, the WP_Post always passed in the first param.

Requirements

By default, the [php_function] is disabled as an added security measure, and requires a constant to be added in order for APL to even access this feature. Also, the first param MUST be a WP_Post object, followed by any other variables you add.

The following is a list you will need access to...

  1. FTP.
    1. wp-config.php file in site root directory.
    2. function.php file in Child Theme.

--- OR ---

  1. WP Dashboard
    1. Plugin with wp-config.php editor.
    2. Child Theme Editor.

PHP Function

The following sections will help explain how to create custom shortcodes.

Modify wp-config.php

In order to modify the wp_config.php, you must either have access to FTP (or cPanel Filemanager), or a plugin that is able to modify the wp-config.php file. Modifying the wp_config.php requires access through a filemanager or FTP client, but won’t be overwritten if any sort of update was to occur.

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define('WP_DEBUG', false);
 
// ADD APL CONSTANT.
define( 'APL_ALLOW_PHP', true );
 
/* That's all, stop editing! Happy blogging. */

WP’s sample can be found at: trunk/wp-config-sample.php

Modify (Child) Theme functions.php

In order to properly add functions without the risk of loosing them. A child theme must exist and be active. If you are unfamiliar with this concept, WP has a tutorial on How to Create a Child Theme.

  1. First, locate the child theme in ../wp-content/themes/THEME-CHILD-NAME/. Requires access to one of the following.
    • Access to FTP (required for creating a child theme).
    • Access to Dashboard -> Theme Editor
  2. Add the custom PHP function. (or for tutorial purposes, add the following example).
    1. Unique function name which is called by php_function shortcode ( ex. [php_function name='FUNCTION_NAME'] )

    2. First param MUST exist for WP_Post object to be passed.

    3. Return variable must be a string.

       function custom_shortcode_example( $post ) {
       	$return = '';
       
       	// DO STUFF HERE.
       	$return .= $post->post_title;
       	$return .= ' by ';
       	
       	$userData = get_userdata( $post->post_author );
       	$return .= $userData->get( $label_type[ $atts_value['label'] ] );
       	// STOP DOING STUFF.
      
       	return $return;
       }
      

Which would output something similar to Hello World by John.

Add [php_function] to preset Post List.

Last but not least, just need to add the php_function shortcode...and that's it. For the example, the following would be added...

[php_function name='custom_shortcode_example']

Would be the equivalent to...

[post_title] by [post_author]

Additional Info

Clone this wiki locally