I'm writing my own php class and I have multiple functions within the class. Something like this: class JSON_API_Hello_Controller { public function test_cat(){ global $json_api; $posts = $json_api->introspector->get_posts(array( 'cat' => 3)); return $this->posts_object_result_bycat($posts); } public function test_cat1() { global $json_api; $posts = $json_api->introspector->get_posts(array( 'cat' => 2)); return $this->posts_object_result_bycat($posts); } protected function posts_object_result_bycat($posts) { global $wp_query; return array( 'count' => count($posts), 'pages' => (int) $wp_query->max_num_pages, 'posts' => $posts ); } public function mix_cat(){ $first_cat = $this->test_cat(); $second_cat = $this->test_cat1(); $json1 = …
Context I'm making a big plugin with a complex architecture. I would like in my plugin architecture split pages and menus building on admin side. So I have these classes : Menu.php, Submenu.php and Page.php, SubPage.php I would like, when the administrator clik on menu/submenu links, it runs a common function callback inside Menu.php and Submenu.php which load only the right page or subpage thanks $_GET['page']. Problem Each submenu link does not bind a subpage anymore. All href of submenu …
Not sure if OOP is covered here but i've copied the class-twentytwenty-customize.php file from the Twenty Twenty parent theme to my child theme and renamed it to class-customize.php because i need to modify it and add more settings to the customizer, but i get errors. I changed line 16 from this class TwentyTwenty_Customize { to this : class TwentyTwenty_Customize_Extended extends TwentyTwenty_Customize { What am i missing I'm loading the class in my child theme like this require get_stylesheet_directory() . '/class-customize.php'; …
Imagine the following script: <?php namespace MySpace; class MyClass { public static function execute() { // Do something post_exists(1,'','','my_post_type'); } } ?> I'm now calling this function at the top of the page template, before any HTML. The problem I get is that wp resolves the post_exists function to the namespace instead of falling back to the global scope, where wp functions are usually recognized in such cases. So I actually get a PHP error saying Call to undefined function …
I created a custom plugin and I need to schedule add_action using Crontrol Plugin, the Setup class is from different php class. But the schedule action cant read the Crontrol even if I execute do_action('schedule') inside functions.php it is not working. class Scheduler { public $setup; public function __construct() { add_action('scheduler', [$this, 'etf_scheduler_func']); } public function etf_scheduler_func() { $this->setup = new Setup(); $this->setup->set_table(); } }
I am trying to add a few meta boxes for a Custom Post Type through a Class. I am using the "register_meta_box_cb" parameter while registering the Custom Post Type to specify the callback function for adding meta boxes. Sample code - (outline of my code) - class ABC { public static function add_customposttype() { $abc_o = new ABC(); $abc_o->reg(); } public function reg() { $args = array( "label" => "ABC", "description" => "New CPT", "public" => true, "register_meta_box_cb" => array($this, …
I have an unusual situation where I need to make the built-in post type 'page' non-hierarchical. I printed the post type object with var_dump(get_post_type_object('page')); die; and I got this: object(stdClass)#164 (26) { ["labels"]=> ... } ["description"]=> string(0) "" ["publicly_queryable"]=> bool(false) ["exclude_from_search"]=> bool(false) ["capability_type"]=> string(4) "page" ["map_meta_cap"]=> bool(true) ["_builtin"]=> bool(true) ["_edit_link"]=> string(16) "post.php?post=%d" ["hierarchical"]=> bool(true) .... } How might I go about modifying the post-type object so that ["hierarchical"]=>bool(false)?
I’m developing an OOP plugin based on WordPress-Plugin-Boilerplate by Devin Vinson. In the includes folder, I’ve created a class for shortcodes: class My_Plugin_Shortcodes { public static function foo_shortcode( $atts ) { // get parameters $a = shortcode_atts( array( 'arg1' => '', 'arg2' => '', ), $atts ); // do something return $myFooVar; } public static function bar_shortcode( $atts ) { // get parameters $a = shortcode_atts( array( 'arg3' => '', 'arg4' => '', ), $atts ); // do something return …
I'm currently developing a custom plugin. All works well on my local dev environment (Local by Flywheel), but, on production environment, I'm unable to activate the plugin. I get this error message: Fatal error: Class 'Activate' not found in /datas/vol3/w4a156338/var/www/my-website/htdocs/wp-content/plugins/ads-toolbox/ads-toolbox.php on line 54 The 'ads-toolbox.php' file it the plugin main php file and it contains the following: function activate_adstoolbox_plugin() { Inc\Base\Activate::activate(); } register_activation_hook( __FILE__, 'activate_adstoolbox_plugin' ); Line 54 is: Inc\Base\Activate::activate(); The class 'Activate' is inside the 'Activate.php' file located in …
What is the best way of using WordPress's core classes in the context of an object-oriented design? I am trying to use $wp_admin_bar to remove a couple of the default WordPress designs but I am not able to find where to add $wp_admin_bar that does not trigger an error. Below is my code. The comments should help to understand what I have tried, and what my thought process was: <?php defined( 'ABSPATH' ) or die('Nothing to see here'); // Since …
Trying to call "admin_notices" hook from a static function. Getting no output. While normal calling to the same hook is working. use Awraq\Init; use Awraq\Notice; /** * Initialize the activation works of the plugin. */ function awraq_activate_plugin() { /* NOT WORKING */ Notice::error('Activation failed'); /* WORKING */ //add_action('admin_notices', 'simplefunction'); } register_activation_hook(__FILE__, 'awraq_activate_plugin'); function simplefunction() { echo '<div class="notice notice-error is-dismissible"> <p>Activation failed</p></div>'; } class namespace Awraq; if (!defined('ABSPATH')) exit; class Notice { private static $class = ''; private static $msg …
The code described below works normally outside of wordpress, but in wordpress I get "warning expects parameter 1 to be valid callback function mycars ($ mycarsclass) not found". If I remove $ mycarsclass and leave only add_node, it works normally. How do I enable this for mycars ($ mycarsclass) to be valid? Is this possible in wordpress? <?php class mycarsclass { public $car1; public $car2; function add_node($args) { echo '<div class="demo">I lake ' . $args['car1'] . 'and' . $args['car2'] . …
I'd like to get a menu object from its theme location argument. My goal is to output separately the menu name and its items name, url and description. Example of what I'm looking for : $menu = get_menu('nav-menu'); //get menu from its theme location echo $menu->name; //displays the menu name foreach($menu->items as $item){ echo '<a href="'.$item->link'">'.$item->name.'</a>'; //displays a link to the item destination echo $item->description; //displays the item description }
I'm pretty stumped on this one. I'm using add_action inside my plugin class to do certain things- add scripts & styles to the head, wp_ajax, etc. Here's the actions, in the __construct: function __construct(){ add_action('admin_menu', array($this, 'sph_admin_menu')); add_action('sph_header', array($this, 'sph_callback')); add_action('sph_header_items', array($this, 'sph_default_menu'), 1); add_action('sph_header_items', array($this, 'sph_searchform'), 2); add_action('sph_header_items', array($this, 'sph_social'), 3); //Below here they don't work. I have to call these outside of the class (but I need class variables within the functions) add_action('wp_print_styles', array(&$this, 'sph_stylesheets')); add_action('wp_print_scripts', array(&$this, 'sph_scripts')); …
Working on my first WordPress plugin and using OOP. I'm trying to create a shortcode that works within a class and confused about how to set it up. With my current setup, the shortcode only prints out the shortcode name on the front end. Here's what I have now inside the class php file: if ( !class_exists( 'month_snapshot' ) ) { class month_snapshot { private $grand_total; public function __construct() { //code here to create grand_total $this->grand_total = $grand_total; } public …
I am trying to write a plugin that will stop a user from publishing (or updating) a post if a given condition is not met, for example, if the title of the post is already being used in a different post (I am using a custom post type, if that makes a difference). I wrote a function that gets called through the 'transition_post_status' hook to attempt to catch the post before it gets published or updated in the database, so …
I've made two custom classes: FooBar BarSchizzle I'm making a quite custom calculation of a bunch of things, that I would like to save in an array in a post_meta field. This is how I save it $obj1 = new FooBar(); $obj2 = new BarSchizzle(); $obj3 = new FooBar(); $arr = [ 'a_key' => $obj1, 'another_key' => $obj2, 'a_third_key' => $obj3, ]; update_post_meta( $post_ID, 'my_custom_field', $arr ); // I also tried this, with same result // update_post_meta( $post_ID, 'my_custom_field', serialize( …
Before anything else, I must say I am new on building WordPress functions. I have a website with online courses. Students have to sit exams and at the end they get a score, an average of all the exams. I am trying to build a simple dynamic PDF diploma with their name and average score, no more, which I will download by clicking on a button from a table/list. I found the class FPDF, unzipped it and uploaded it through …
I'm writing a new plugin, and one of the things it does is create a new dynamic block. I generally write my plugins based off of WPPB, which does things in an object-oriented way, and semantically separates admin functionality from public functionality. I have the admin class successfully creating the edit side of the block. But for the "save" side, for a dynamic block, it makes semantic sense for the render function to be in the public class ... but …
I've this in my main plugin file: $main_admin_class = new MainAdminClass(); $main_admin_class->init(); MainAdminClass has this code: class MainAdminClass { public function init() { $child_admin = new ChildAdminClass(); $child_admin->init(); } } and, finally, the child class has this code class ChildAdminClass { public function init() { add_action('admin_notices', array($this, 'printFoo')); //I know it works if I use 'ChildAdminClass' instead of $this } public function printFoo() { echo 'fooooooooooooooooooooooooooooo'; } } When later I try to use remove_action in functions.php, with this code: …