I am trying to extend a background image control in the Wordpress theme customizer. I have been trying to get this code right and now that I think it should be working I am receiving an unexpected T_Public on this function:
public function tab_builtins() {
I have attempted to remove the public declaration before it, although that creates a new issue: Unexpected t_function
I have been looking at this code for a long time trying to change minor things but the issue remains. Can anyone help me out?
Here is the full code in question:
function WP_Customize_Background_Image_Control_Defaults($wp_customize) {
/* Substitute the default control for our new one */
$wp_customize->remove_control( 'background_image' );
$wp_customize->add_control( new WP_Customize_Background_Image_Control_Defaults( $wp_customize ) );
class WP_Customize_Background_Image_Control_Defaults extends WP_Customize_Background_Image_Control {
public function __construct( $manager ) {
$this->add_tab( 'builtins', __('Built-ins'), array( $this, 'tab_builtins' ) );
public function tab_builtins() {
$backgrounds = array(
'/wp-content/themes/newtheme/img/backgrounds/background1.jpg', '/wp-content/themes/newtheme/img/backgrounds/background2.jpg', '/wp-content/themes/newtheme/img/backgrounds/background3.jpg', '/wp-content/themes/newtheme/img/backgrounds/background4.jpg', '/wp-content/themes/newtheme/img/backgrounds/background5.jpg'
);
if ( empty( $backgrounds ) )
return;
foreach ( (array) $backgrounds as $background )
$this->print_tab_image( esc_url_raw( $background->guid ) );
}
}
}
}
add_action( 'customize_register', 'wp_customize_background_image_control_defaults', 11, 1 );
You have missed the closing curly brace right before public function tab_builtins()
Put it to close your __construct definition properly
Related
I have a class sitting in my Wordpress functions.php. Eventually it's going to end up in the plugins folder but one step at a time. Below is a foreshortened version of it:
class metaboxClass {
$them_meta_boxes = array (
array (
"1a_myplugin_box_id_1",
"1b_Custom Meta Box Title 1"
),
array (
"2a_myplugin_box_id_2",
"2b_Custom Meta Box Title 2"
)
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
}
public function add_meta_box( $post_type ) {
$post_types = array( 'page', 'my_cpt' );
if ( in_array( $post_type, $post_types )) { // *** IF $POST_TYPE IS IN THE ARRAY $POST_TYPES
foreach ($this->them_meta_boxes as $level_1) {
add_meta_box (
foreach ($this->level_1 as $level_2) {
echo $level_1 . ",";
}
array( $this, 'render_form'),
$post_type
)
}
}
}
}
As you can see from the above, I'm trying to construct various iterations of the add_meta_boxes function using the information in the array.
I've a feeling that there are a number of issues here and I'm kind of going through them one at a time but the first is that when an object is instantiated from the class I get: "syntax error, unexpected 'foreach' ". I know this is usually caused by a missing semi-colon. In this case the semi colon is present and correct. I've a feeling it's something to do with the placement of the array but I'm getting similar problems when it's placed outside. Can anyone give me any pointers - I'm pretty new to the world of OO PHP and also to really getting my hands dirty with the wordpress backend so any pointers would be appreciated.
Thanks in advance,
Stef
You can't pass a foreach loop as a parameter to a function. Construct your argument string first and then pass the constructed string as an argument to your add_meta_box function.
Even then though, I'm not sure what you are trying to call since your add_meta_box function only takes one argument.
Got it sorted for the record... ended up something like this:
class initialise_meta_boxes {
public $meta_boxes_array = array (
array (
"1a_myplugin_box_id_1",
"1b_Custom Meta Box Title 1",
"render_dropdown"
),
array (
"2a_myplugin_box_id_2",
"2b_Custom Meta Box Title 2",
"render_dropdown"
)
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}
public function make_meta_box($meta_id, $meta_title, $meta_callback) {
return add_meta_box ($meta_id, $meta_title, $meta_callback, $post_type );
}
public function add_meta_box( $post_type ) { // *** $post_type is global variable!!!
$post_types = array( 'page', 'my_cpt' );
if ( in_array( $post_type, $post_types )) { // *** IF $POST_TYPE IS IN THE ARRAY $POST_TYPES
foreach ($this->meta_boxes_array as $value) {
$this->make_meta_box($value[0], $value[1], array( $this, $value[2]));
}
}
}
}
This question already has answers here:
The plugin generated X characters of unexpected output during activation (WordPress)
(25 answers)
Closed 4 years ago.
I want to create a wordpress plugin by just following the example listed here based on a class OOP architecture with an external setup object, and adapting the source code on my own way like this:
main plugin file:
<?php
/*
Plugin Name: My Plugin
Description: My Plugin
Version: 1.0
Author: drwhite
Author URL: drwhite-site
Plugin URL: drwhite-site/video-ad-overlay
*/
register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
register_deactivation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_deactivation'));
register_uninstall_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_uninstall'));
add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
class VAO_Setup_File{
protected static $instance;
public static function init()
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( current_filter(), array( $this, 'load_files' ));
}
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
}
}
In my plugin root directory i have created a subdirectory called includes within i put the setup file to be loaded on plugin load called setup.class.php:
<?php
class VAO_Setup_File_Inc
{
public static function on_activation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
}
public static function on_deactivation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
}
public static function on_uninstall()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
check_admin_referer( 'bulk-plugins' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
}
}
When i activate the plugin i got an error like the following:
I have read several questions posted by other users here and this may be duplicated question, but any of suggested answer worked for me including:
Remove space from start of tags and even remove the php end tag: nothing changed
in wp_config.php file i set wp_DEBUG to true , but it doesn't show errors
I have converted the file to UTF8 (without BOM) nothing changed
Have you put the eye in the issue ?
You are getting this error because your plugin is generating a PHP error that is being outputted to the page and causing the headers sent error you see... The problem with your code is that your function
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
}
is not being called in time, so
register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
is looking for a function that doesn't exist, inside a class that doesn't exist. Move your
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
outside the class altogether, and it'll load just fine. It may require you to rethink your use of
add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
and the way your plugin is being created, but it's a step in the right direction. If you copy and paste the code from the link you got this code from, his code displays the same problem...
I tried to declare a new time interval for a WP_CRON.
When I do it in a PHP file it works.
When I do it in a PHP Class it doesn't.
Can someone see what I'm doing wrong ?
I'm using the plugin cron view to check if the declaration is working or not.
If I solve this problem I think it will also solve my problem to know why my cron job is not triggered in the class but works properly when not in a class.
=> File myplugin.php
function set_up_option_page() {
add_options_page( [...]);
}
add_action( 'admin_menu', 'set_up_option_page' );
function do_some_rock() {
$instance = My_Plugin_Class::instance();
if ( isset($_POST['action']) && 'do-magic' == $_POST['action'] ) {
$instance->do_stuff();
}else{
// Display the form.
<?
}
}
=> File My_Plugin_Class.php
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class My_Plugin_Class {
private static $_instance = null;
public function __construct () {
[...]
add_filter( 'cron_schedules', array($this,'cron_time_intervals'));
}
public function cron_time_intervals( $schedules ) {
echo "——— cron time intervals —— ";
$schedules['minutes_1'] = array(
'interval' => 10*60,
'display' => 'Once 10 minutes'
);
return $schedules;
}
public static function instance () {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} // End instance ()
Best regards.
I am almost sure that do_some_rock() is not called every time - only when someone goes to that page. You can move add_filter( 'cron_schedules', array($this,'cron_time_intervals')); from your class constructor to main plugin file myplugin.php and do more or less something like this
add_action( 'admin_menu', 'set_up_option_page' );
$instance = My_Plugin_Class::instance();
add_filter( 'cron_schedules', array($instance ,'cron_time_intervals'));
One extra question. Does My_Plugin_Class constructor contain any code you would like to prevent being executed during each request?
I think the proper way to do this, is to have a specific class for my Cron.
This class is instanciated on each request this is why the code shouldn't be in a class with a static instance as I did before.
Also I think it's better to have the Cron outside the plugin class for logic and cleaner code purpose. Thanks to Lukas Pawlik for the help.
if (!defined('ABSPATH')) exit;
new My_Cron();
class My_Cron {
public function __construct() {
add_filter('cron_schedules', array($this, 'cron_time_intervals'));
add_action( 'wp', array($this, 'cron_scheduler'));
add_action( 'cast_my_spell', array( $this, 'auto_spell_cast' ) );
}
public function cron_time_intervals($schedules)
{
$schedules['minutes_10'] = array(
'interval' => 10 * 60,
'display' => 'Once 10 minutes'
);
return $schedules;
}
function cron_scheduler() {
if ( ! wp_next_scheduled( 'cast_my_spell' ) ) {
wp_schedule_event( time(), 'minutes_10', 'cast_my_spell');
}
}
function auto_spell_cast(){
My_Plugin_Class::instance()->launch_spell();
}
}
I am not Sure But Try this.
add_filter( 'cron_schedules', array($this,'cron_time_intervals'),1);
And Check cron_time_intervals() Function is Call or Not.
I'm trying to develop my first Wordpress plugin and I got staled in the very first stage. I'm trying to setup some options and database tables when the plugin is activated, but no luck. No matter what I do, the plugin activates, but the database is untouched and the options are not stored in DB. I tried echoing inside the constructor, but it seems that it never reaches it. I have debug activated in WP, but no error is being reported. The function is not being hooked. Does someone can spot what's wrong with my code?
Thanks for any help in advance.
class Myplugin {
private static $instance;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
register_activation_hook( __FILE__, array( &$this, 'plugin_activate' ) );
}
public function plugin_activate() {
if ( version_compare( get_bloginfo( 'version' ), '3.8.2', ' < ' ) ) {
deactivate_plugins( basename( __FILE__ ) );
} else {
$rlm_rsvplus_options = array(
'db_version' => '1.0',
'event_name' => '',
'end_date' => '',
);
update_option( 'rlm_myplugin_options', $rlm_myplugin_options );
require_once( "includes/rlm_myplugin_db_setup.php" );//It never reaches this file.
}
}
}
$myplugin = Myplugin::get_instance();
Back to the WordPress documentation.
<?php register_activation_hook( $file, $function ); ?>
Parameters
$file
(string) (required) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.
Default: None
$function
(callback) (required) The function to be run when the plugin is activated. Any of PHP's callback pseudo-types will work.
Default: None
Possible issue
If calling a function from a file that is outside of main plugin file, then the hook will not work as it is not pointing to the right file. FILE will point to the file where the code is written. So if you happen to include this part of code from elsewhere (another file - not the main plugin file) it's not supposed to work unless you point the right path.
Solution
A solution could be declaring a constant in the main plugin file.
your_main_plugin_file.php
define(PLUGIN_FILE_URL, __FILE__);
and then use it in your included file like so.
includes/some_file.php
<?php register_activation_hook( PLUGIN_FILE_URL, ['your_class_name_here', 'your_class_method_name_here']); ?>
or if you use functions instead of classes then do
<?php register_activation_hook( PLUGIN_FILE_URL, 'your_function_name_here'); ?>
The register_activation_hook call needs to be outside of the class itself.
Something like:
class Myplugin {
private static $instance;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
// do other stuff here
}
public function plugin_activate() {
if ( version_compare( get_bloginfo( 'version' ), '3.8.2', ' < ' ) ) {
deactivate_plugins( basename( __FILE__ ) );
} else {
$rlm_rsvplus_options = array(
'db_version' => '1.0',
'event_name' => '',
'end_date' => '',
);
update_option( 'rlm_myplugin_options', $rlm_myplugin_options );
require_once( "includes/rlm_myplugin_db_setup.php" );
}
}
register_activation_hook( __FILE__, array( 'Myplugin', 'plugin_activate' ) );
You can read more on the following tutorial by Francis Yaconiello about How to write WordPress plugin.
In order to work register_activation_hook OR register_deactivation_hook the functions should be in index file OR we need to specify the full path to the file argument.
Replace this:
register_activation_hook( FILE, array( &$this, 'plugin_activate' ) );
With:
register_activation_hook( FILE . 'plugin-main-file.php', array( &$this, 'plugin_activate' ) );
Here the point is register_activation_hook( $file, $function );
Here $file means Path to the main plugin file
Reference: https://codex.wordpress.org/Function_Reference/register_activation_hook
Thanks,
- Adi
I'm having a problem creating a class called API_Widgets in WordPress using the magic __call function. If I simple rename the file to derp.php and the class to API_Derp then it works without problems.
The following example has been stripped of everything unimportant to this issue (so if there is any error other than the specific fatal error specified in the third code block, ignore it).
Please keep in mind that I know the core.php or API class' __call works, as renaming the widgets.php or invoking another class works just fine.
core.php:
class API {
function __call( $method, $args ) {
$rmethod = "API_{$method}";
if ( !class_exists( $rmethod ) ) {
$lmethod = strtolower( $method );
require_once( "{$lmethod}.php" );
}
return new $rmethod( $args );
}
}
widgets.php:
class API_Widgets {
private $widgets = array();
function Add( $widgets ) {
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( !in_array( $widget, $this->widgets ) )
$this->widgets[] = $widget;
}
} else
$this->widgets[] = $widgets;
}
}
api.php:
$api = new API();
$widgets = $api->Widgets(); // Fatal error: Class 'API_Widgets' not found in /home4/goldencr/public_html/wp-content/plugins/minecraft-api/api/core.php on line 25
//$widgets->Add( 'some_widget' );
Extending from comment:
Though not hinted at your question, it seems you may not actually included the widgets.php. Try to use absolute path to fix that:
require_once( __DIR__.DIRECTORY_SEPARATOR.$lmethod.".php" );