Object function as callback for add_shortcode( ) in WordPress - php

I created a class to build custom post types and I'm trying to include a way to insert specific shortcodes for that post type, but I can't seem to get the callback function for the add_shortcode() to get a function from inside the created object.
I have something similar to this:
class custom_post_type {
public $post_type_name;
function __construct($id,$options) {
//builds the custom post type, etc
$this->post_type_name = $id;
$shortcode = new build_shortcode($id,$options);
}
public function shortcode_handler($atts) {
return $this->post_type_name;
}
}
class build_shortcode {
function __construct($id,$options) {
add_shortcode( $options['shortcode_name'] , array( $id ,'shortcode_handler') );
}
}
$options = array(); // a bunch of variables would be defined here to use in the classes
$post_type = new custom_post_type('post_type',$options);
But when I use the [show-cpt-name] shortcode, it doesn't run the function.
I know in the Codex they give and example to use a class, like:
add_shortcode( $options['shortcode_name'] , array( 'custom_post_type' ,'shortcode_handler'));
But this is not what I am looking for, since I wanted the callback function to use values from that particular object created.
Is this possible to achieve?

Related

Extend PHP Class to allow new methods found via __callStatic

Looking for a flexible way to allow other developers to extend render methods for a templating system, basically allowing them to generate their own render::whatever([ 'params' ]) methods.
Current set-up work well from a single developer point of view, I have a number of classes set-up based on context ( post, media, taxonomy etc ), with a __callStatic method collecting the calling function which checks if the method_exists within the class, and if so, extracts any passed arguments and renders the output.
quick example ( pseudo-code ):
-- view/page.php
render::title('<div>{{ title }}</div>');
-- app/render.php
class render {
public static function __callStatic( $function, $args ) {
// check if method exists
if ( method_exists( __CLASS__, $function ){
self::{ $function }( $args );
}
}
public static function title( $args ) {
// do something with the passed args...
}
}
I want to allow developers to extend the available methods from their own included class - so they could create for example render::date( $args ); and pass this to their logic to gather data, before rendering the results to the template.
The questions is, what approach would work best and be performant - errors are safety are not a big concern at this point, that can come later.
EDIT --
I am already making this work by doing the following ( pseudo-code again.. ):
-- app/render.php
class render {
public static function __callStatic( $function, $args ) {
// check if method exists
if (
method_exists( __CLASS__, $function
){
self::{ $function }( $args );
}
// check if method exists in extended class
if (
method_exists( __CLASS__.'_extend', $function
){
__CLASS__.'_extend'::{ $function }( $args );
}
}
public static function title( $args ) {
// do something with the passed args...
}
}
-- child_app/render_extend.php
class render_extend {
public static function date( $args = null ) {
// do some dating..
}
}
The issue here is that this is limited to one extension of the base render() class.
A common way (used by Twig and Smarty, for a couple of examples), is to require developers to manually register their extensions as callables. The render class keeps a record of them, and then as well as checking its own internal methods, also checks this list from _callStatic.
Based on what you have already, this might look like this:
class render
{
/** #var array */
private static $extensions;
public static function __callStatic($function, $args)
{
// check if method exists in class methods...
if ( method_exists( __CLASS__, $function )) {
self::{$function}(self::$args);
}
// and also in registry
elseif (isset(self::$extensions[$function])) {
(self::$extensions[$function])($args);
}
}
public static function title($args)
{
// do something with the passed args...
}
public static function register(string $name, callable $callback)
{
self::$extensions[$name] = $callback;
}
}
A developer would make use of this like so:
render::register('date', function($args) {
// Do something to do with dates
});
Full demo here: https://3v4l.org/oOiN6

Render .twig file when shortcode called in WordPress?

I've been firefighting this for a while and I'm struggling with getting this shortcode to render this Twig partial when it is called. Anyone have any advice?
Thanks!
Here's what I have so far:
<?php namespace loc\wp\lib;
use \Timber as Timber;
use \Twig_SimpleFunction as Twig_SimpleFunction;
class LOCShortcodes {
public function addSlider() {
add_shortcode('feature-slider', 'feature_slider');
function feature_slider() {
return Timber::render('template-feature-slider.twig');
}
}
}
When you work with hooks and filters (or shortcodes in your case) in a class context, you need to define the callback a little differently.
The last example in https://codex.wordpress.org/Function_Reference/add_shortcode shows you how you use shortcodes with classes:
<?php
class MyPlugin {
public static function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );
As you can see, the shortcode is added outside of the class. If you want to add it inside your class, then you don’t have to explicitly use the name of the class, but you can use $this:
<?php
class MyPlugin {
public function __construct() {
add_shortcode( 'baztag', array( $this, 'baztag_func' ) );
}
public static function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
}
In your case, you could do it like this:
<?php
class LOCShortcodes {
public function __construct() {
add_shortcode( 'feature-slider', array( $this, 'feature_slider' ) );
}
public function feature_slider() {
return Timber::compile( 'template-feature-slider.twig' );
}
}
Don’t forget to use Timber::compile() instead of Timber::render(), because the render() function is echoing the output, while for shortcodes, the output should be returned. This is also mentioned in the Notes section in the Codex:
Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. […]
Also make sure to read the wiki section about Shortcodes in Timber.

PHP function inside another function [ class ]

I have the following code, perform a global function within a class to fill the functions of wordpress, the problem is that the only way that I could get a variable public class is as follows
class Core {
public $notice;
function __construct(){
$this->core_function();
}
function core_function(){
global $globalvar;
$globalvar = $this;
function notice_global(){
global $globalvar;
return $globalvar->notice;
}
}
function set_notice(){
$this->notice = array('Warning');
}
}
$GP = new Core();
$GP->set_notice();
var_dump(notice_global());
Any other ideas or suggestions, this code is correct or not?
As you said in the comments, you need global function due to wordpress hook method (for a plugin, I suppose).
This is not necessary: there is a way to pass an object method (not a whole object) to wordpress.
You can try in this way:
class Core {
public $notice;
function get_notice()
{ return $this->notice; }
function set_notice()
{ $this->notice = array('Warning'); }
}
$GP = new Core();
$GP->set_notice();
add_action( 'save_post', array( $GP, 'get_notice' ) );
Or - for a better flexibility - in this way:
class Core {
public $notice;
function get_notice()
{ return $this->notice; }
function set_notice()
{ $this->notice = array('Warning'); }
function add_wp_action( $hook, $method )
{ add_action( $hook, array( $this, $method ) ); }
}
$GP = new Core();
$GP->set_notice();
$GP->add_wp_action( 'save_post', 'get_notice' );
By this way, you can directly set all your wp hooks in the class and call they directly with an object method, without using globals variables or function tricks.
I'm not sure if I'm understanding you right, but notice_global can be moved out of that class.
Globals have scope outside of classes
There is no need for these functions. You've defined $notice as public property. You can access it like this: $GP->notice;
You might also want to read documentation on visibility of methods and properties.

Wordpress plugin: Call to undefined function add_menu_page()

I need some help, I've been through so many of these "similar" issues on stackoverflow and all over the web but nothing seems to be helping.
I'm new to wordpress and started creating a plugin using classes. when I run the plugin it throws the error "Call to undefined function add_menu_page()". I've checked the support docs on wordpress.org but that doesn't help either (https://codex.wordpress.org/Adding_Administration_Menus#Inserting_the_Pages)
heres my code below please can you tell me what I'm doing wrong:
class SetupAdminMenu {
public static function add_menu($menu)
{
add_action('admin_menu', self::load_menu($menu) );
//I've used it according to the docs as well
//add_action('admin_menu', array(self, self::load_menu($menu) );
}
public static function load_menu($menu)
{
$page = add_menu_page( $menu['page_title'], $menu['menu_title'], $menu['capability'], $menu['menu_slug'],
array(self,self::load_view($menu['function'])), $menu['icon_url'], $menu['position'] );
self::load_page($page);
}
protected static function load_view($view)
{
if ( !current_user_can( 'activate_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//The actual page to display
include_once($view);
}
protected static function load_page($page)
{
add_action( 'admin_print_styles-' . $page, 'admin_styles' );
}
}
Thanks peeps
Couple of things going on here. First to just clarify, add_menu_page is to add an administration menu page for managing settings, etc.
Your add action should be as follows:
public static function add_menu($menu)
{
add_action('admin_menu', array('SetupAdminMenu', 'load_menu'));
}
The way WordPress handles actions requires you to format your add_action as follows for static class methods:
add_action('admin_menu', array('ClassName', 'methodname'), $priority, $numVars);
There are two optional variables:
$priority = integer, If there are multiple actions to a hook, the priority will determine the order of load. Default is 10. Use 99 if you want to react to any earlier actions, use 1 if you want to define things before further actions
$numVars = interger, if the method or function requires more then one variable, put the number here. WordPress core hooks don't require this to be defined and will pass variables according to the documentation.
Next, the class method with "add_menu_page" is pretty off. To my knowledge admin_menu hook does not return any variables by default and even if it did, why do you want the existing menu when trying to add new items? You pass those variables yourself:
public static function load_menu()
{
$page_title = 'My New Page';
$menu_title = 'New Page';
$capability= 'manage_options'; //An administrator only capability
$menu_slug = 'my-new-page';
$callback = array('SetupAdminMenu', 'load_view');
$menu_icon = ''; //use default
$menu_position = 81 //After settings
add_menu_page( $page_title, $menu_title, $capability, $menu_slug,
$callback, $menu_icon , $menu_position);
}
Your self::load_page($page); is redundant. The action you use in that function does nothing. The callback is what is displayed when the menu item is clicked. No reason to make that protected, it would likely fail anyway because of the way class methods are called from hooks within WordPress.
public static function load_view() // Removed $view because you need to define this yourself.
{
if ( !current_user_can( 'activate_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//The actual page to display
//include_once('/path/to/your/template');
//which should have php, html for what you want to have appear.
//To test that the rest of the code is working... just put:
echo 'Hello World!';
}
This link has a list of a bunch of "how tos" on WP Plugin development.
alright, so I've done some more reading and playing and below is my answer, might help some.
class SetupAdminMenu {
private static $menu = array();
public static function add_menu()
{
add_action('admin_menu', array('SetupAdminMenu', 'load_menu') );
}
public static function add_sub_menu()
{
add_action('admin_menu', array('SetupAdminMenu', 'load_sub_menu') );
}
the method below is what I was missing initially, because you cannot pass an argument from the call back in add_action()
public static function setupPage($params)
{
self::$menu = $params;
}
public static function load_menu()
{
$page = add_menu_page( self::$menu['page_title'], self::$menu['menu_title'], self::$menu['capability'],
self::$menu['menu_slug'], array('SetupAdminMenu', 'load_view'),
self::$menu['icon_url'], self::$menu['position'] );
self::load_page($page);
}
protected static function load_sub_menu()
{
$page = add_submenu_page( self::$menu['parent_slug'], self::$menu['page_title'], self::$menu['menu_title'],
self::$menu['capability'], self::$menu['menu_slug'], 'load_view');
self::load_page($page);
}
public static function load_view()
{
if ( !current_user_can( 'activate_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//The actual page to display
include_once(self::$menu['page']);
}
protected static function load_page($page)
{
add_action( 'admin_print_styles-' . $page, array('SetupAdminMenu', 'admin_styles') );
}
public static function admin_styles()
{
wp_register_style( 'admin_styles', plugins_url('css/admin.css', __FILE__) );
wp_enqueue_style( 'admin_styles' );
}
}
because add_action() is used so many times I've removed add_menu() and add_sub_menu() from this class and added the method below to may base class to be called with the necessary parameters for any actions
public static function addAction($action, $callback)
{
add_action($action, $callback);
}
might not be 100% the best way to do it yet but the above currently works.

Accessing SilverStripe custom config variable from controller that extends page

Basically everything works if I hard code the URL in my Ajax_Controller, but I want it to access the URL from the CMS field I created.
Thanks in advance. (please ignore when I don't close my braces - just trying to copy / paste efficiently)
In /mysite/_config.php I created a custom config:
Object::add_extension('SiteConfig', 'CustomSiteConfig');
In /mysite/code/CustomSiteConfig.php I added a field where I'll store a URL:
class CustomSiteConfig extends DataObjectDecorator {
function extraStatics() {
return array(
'db' => array(
'COJsonPath' => 'Text'
)
);
}
public function updateCMSFields(FieldSet &$fields) {
$fields->addFieldToTab("Root.CO", new TextField("COJsonPath", "CO JSON URL"));
}
public function getCOJsonPath(){
return $SiteConfig.COJsonPath;
}
This successfully creates a tab in the main parent in the CMS called "CO" and a field named "CO JSON URL". I logged into my CMS and saved http://api.localhost/mymethod/ to that field.
Now I have created an Ajax page type to facilitate running Ajax commands without letting the web site user find where my APIs are, and because jQuery Ajax no likey XSS (cross site scripting).
In /mysite/code/Ajax.php:
class Ajax extends Page {
static $db = array(
);
static $has_one = array(
);
function getCMSFields()
{
$fields = parent::getCMSFields();
return $fields;
}
}
class Ajax_Controller extends Page_Controller {
public function getCO()
{
$buffer = self::createHttpRequest("http://api.localhost/mymethod/");
//$buffer = self::createHttpRequest($CustomSiteConfig::getCOJsonPath());
return $buffer;
}
This code works, but when I try to execute my createHttpRequest() with the line you see commented out, it fails. I know my syntax is wrong, I just can't figure out what it should be. Thanks for helping - I've done this before I just can't figure it out - its Friday.
I spotted several syntax errors in your code:
public function getCOJsonPath(){
return $SiteConfig.COJsonPath;
}
should be:
public function getCOJsonPath(){
return $this->owner->COJsonPath;
}
1) $SiteConfig is never defined at that point.
2) usually you would use $this, but in your case you are inside a DataObjectDecorator, so you have to use $this->owner
3) you can not use . to access properties of an object, in php you have to use ->
moving on to class Ajax_Controller, inside getCO there are the following errors:
1) $CustomSiteConfig is not defined, therefore can not be used
2) getCOJsonPath is not a static function, but you try to call it as static (again you have to use ->
so, the code should look something like this:
public function getCO() {
$siteConfig = SiteConfig::current_site_config();
$buffer = self::createHttpRequest($siteConfig->getCOJsonPath());
return $buffer;
}
that should work, but there is another think that could be improved.
As I understand it, you are creating an ajax page, which you then create once in the CMS and tell your website content authors never to touch the ajax page?
This is quiet ugly, and there are several nice ways to do what you want to do.
Here is how I would create an Ajax controller:
_config.php
// tell SilverStripe what URL your AjaxController should have,
// here we set it to AjaxController::$URLSegment which is 'myAjaxController'
// so the url to the controller is mysite.com/myAjaxController
Director::addRules(100, array(
AjaxController::$URLSegment => 'AjaxController',
));
AjaxController.php
<?php
class EventAssetsController extends Controller {
public static $URLSegment = 'myAjaxController';
// tell SilverStripe what URL should call what function (action)
// for example, mysite.com/myAjaxController/foo should call the function foo
public static $url_handlers = array(
'foo' => 'foo',
'bar/$ID/$OtherID' => 'bar',
'co' => 'getCO'
);
public function Link($action = null) {
// this function is just a helper, in case you evern need $this->Link()
return Controller::join_links(self::$URLSegment, $action);
}
public function AbsoluteLink($action = null) {
return Director::absoluteURL($this->Link($action));
}
public function foo(SS_HTTPRequest $request) {
// do something here
// this method is an action, the url to this action is:
// mysite.com/myAjaxController/foo
}
public function bar(SS_HTTPRequest $request) {
// do something here
// this method is an action, the url to this action is:
// mysite.com/myAjaxController/bar
// you notice that the $url_handlers has "bar/$ID/$OtherID",
// that means you cann call mysite.com/myAjaxController/bar/21/42
// and silverstripe will make 21 the ID, and 42 the OtherID
// you can access ID and OtherID like this:
// $ID = $request->param('ID'); // which is 21
// $OtherID = $request->param('OtherID'); // which is 42
}
public function getCO() {
// this method is an action, the url to this action is:
// mysite.com/myAjaxController/co
$siteConfig = SiteConfig::current_site_config();
$buffer = self::createHttpRequest($siteConfig->getCOJsonPath());
return $buffer;
}
}

Categories