Getting a Syntax Error while calling the function in PHP CODE - php

I have been developing a website in wordpress and when i am fetching the functgions...the dbppwl_script() it response wbut when im trying to ru the after theme functgion it shows an syntax error related to the fucntion after_theme
<?php
// theme function code
function dbpplwp_theme_setup(){
add_theme_support('custom-logo');
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_image_size('home-featured', 680, 400, array('center','center'));
add_theme_size('single-img', 600, 550, array('center','center'));
add_thehe_support('automatic-feed-links');
register_nav_menus( array(
'primary' => _('Primary Menu','dbpplwp')
));
}
add_action('after_setup_theme', 'dbpplwp_theme_setup' )
function dbpplwp_scripts(){
wp_enqueue_style('style',get_stylesheet_uri() );
};
add_action('wp_enqueue_scripts','dbpplwp_scripts');
?>
Can anybody tell what kind of error is recurring here

You missed ; Each statement must ends up with a semicolon in PHP.
missed here : add_action('after_setup_theme', 'dbpplwp_theme_setup' )
to
add_action('after_setup_theme', 'dbpplwp_theme_setup' );

Related

WordPress which is the right way to enqueue script and style in a php class? [duplicate]

So I'm busy in Wordpress. I have a theme for the site and all. It's all working fine. Then I want to activate the new theme. BENG BENG, white screen of death. I debugged and this is the error:
Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or init hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in C:\xampp\htdocs\wp-includes\functions.php on line 2944
Line 2944 is just the line that throws an error. I already checked that.
Anyone ever experienced and solved this?
EDIT:
Referencing lines:
function _doing_it_wrong( $function, $message, $version ) {
do_action( 'doing_it_wrong_run', $function, $message, $version );
// Allow plugin to filter the output error trigger
if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
$version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
$message .= ' ' . __( 'Please see Debugging in WordPress for more information.' );
trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
}
}
The error have nothing related to the code you've added added. It is not a Wordpress core related issue, but an issue with your theme or a plugin
What the error means is that some script is enqueued way too early, ie, wp_enqueue_script() is hooked to a wrong hook that runs before wp_enqueue_scripts, admin_enqueue_scripts, or init.
Unfortunately this error in Wordpress is a bit fague as it doesn't tell you exactly where the problem is, just that wp_enqueue_script is wrongly called.
You'll need to look for all instances in your theme and plugins for wp_enqueue_script and check that it is properly hooked
EDIT
From your comments, you have found three instances of wp_enqueue_script. You now need to see how it is hooked. It should look something like this
function some_function_name(){
wp_enqueue_script(ALL THE SCRIPT DETAILS IN HERE);
}
add_action( 'THIS IS THE HOOK THAT IS IMPORTANT', 'some_function_name');
THIS IS THE WRONG HOOK USED is what you must check, as this is the wrong hook. This must be wp_enqueue_scripts or admin_enqueue_scripts, depending on if the script is meant for front end or back end
In some of your plugins or your theme, a script may be included like this :
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
try to change it using this code:
<?php
function wpb_adding_scripts() {
wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
?>

What is the correct way to convert a static gutenberg block created using #wordpress/create-block into a dynamic block registering it using PHP?

I've been searching long and hard to find the answer to this but every tutorial or advice I follow so far has let me down.
I'm trying to convert a static Gutenberg block I created for WordPress into a dynamic block. To do this the block must be registerd in PHP using register_block_type() instead of using block.json.
According to every tutorial I have read all I should need to do is change the register_block_type function inside the plugins main php file.
In the PHP file for the static version which works I did have...
function wholesomecode_wholesome_plugin_block_init() {
register_block_type( __DIR__ );
}
add_action( 'init', 'wholesomecode_wholesome_plugin_block_init' );
I then updated the register_block_type function to this...
function wholesomecode_wholesome_plugin_block_init() {
if ( ! function_exists( 'register_block_type' ) ) {
// Block editor is not available.
return;
}
register_block_type( 'wholesomecode/wholesome-plugin', [
'attributes' => [
'blockText' => [
'default' => 'Wholesome Plugin - hello from the editor!',
'type' => 'string',
],
],
'editor_script' => 'wholesomecode-wholesome-plugin-block-editor',
'editor_style' => 'wholesomecode-wholesome-plugin-block-editor',
'render_callback' => function( $attributes, $content ) {
$block_text = esc_html( $attributes['blockText'] );
return "<p class='wp-block-wholesomecode-wholesome-plugin'>$block_text</p>";
},
'style' => 'wholesomecode-wholesome-plugin-block',
]
);
}
add_action( 'init', 'wholesomecode_wholesome_plugin_block_init' );
In index.js in the src directory I have also updated the save method so it returns null.
Now when I build and run the block it doesn't show up in the block editor in WordPress.
If anyone you could help me you'd definitely be saving me a few lost hairs and sleepsless nights becuase I've trying at this for days now.
A block created with #wordpress/create-block can be converted to a dynamic block with only a few minor changes to the generated files. I would suggest recreating your block to get back to the original files/project structure before making these changes:
Create a render callback function, eg render_create_block_wholesome_plugin() to handle the server side rendering of your block. This could be in either in the main plugin file or a separate php file included in the main plugin.
<?php
// Render callback function
function render_create_block_wholesome_plugin($attributes, $content)
{
$block_text = esc_html($attributes['blockText']);
return sprintf('<p class="wp-block-create-block-wholesome-plugin">%1$s</p>', $block_text);
}
In the main plugin file, add render_callback with the name of your function to register_block_type() to render the block.
wholesome-plugin.php
function create_block_wholesome_plugin_block_init() {
// Using the built version of block.json to define block + PHP render callback
register_block_type(__DIR__ . '/build', array(
'render_callback' => 'render_create_block_wholesome_plugin'
));
}
add_action( 'init', 'create_block_wholesome_plugin_block_init' );
All other block properties should remain in block.json. From WordPress 5.8 using block.json for defining blocks is the recommended approach.
block.json
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "create-block/wholesome-plugin",
"attributes": {
"blockText": {
"default": "Wholesome Plugin - hello from the editor!",
"type": "string"
}
},
...
"textdomain": "wholesome-plugin",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css"
}
As the block is now rendered by PHP, save.js can be safely deleted from your project. Also remember to remove import save from './save'; from index.js and update registerBlockType(), eg:
index.js
import { registerBlockType } from '#wordpress/blocks';
import './style.scss';
/**
* Internal dependencies
*/
import Edit from './edit';
registerBlockType( 'create-block/wholesome-plugin', {
/**
* #see ./edit.js
*/
edit: Edit,
// Remove save - default is null, no need to add return null
} );
After making the changes above, build/run your project and now you should have a basic dynamic block setup ready for further development..

WP_Ajax_UnitTestCase does not throw WPAjaxDieStopException

Ok so I am testing my ajax callbacks for my wordpress plugin.
So I basically followed instructions here
https://codesymphony.co/wp-ajax-plugin-unit-testing/
Here is my ajax callback function
public function my_plugin_get_site_pages( $args = null ) {
//...... Processing $site_pages.....
$response = array(
'status' => 'success',
'site_pages' => $site_pages
);
#header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
echo wp_json_encode( $response );
wp_die();
}
Here is my test
class My_Plugin_Ajax_Test extends WP_Ajax_UnitTestCase {
private $_foo;
public function setup() {
//.....Initialize $_foo here...
}
public function test_foo() {
try {
$_POST[ 'args' ] = array( 'return_format' => 'raw' );
add_action( 'wp_ajax_my_plugin_get_site_pages' , array( $this->_foo , 'my_plugin_get_site_pages' ) );
//$this->setExpectedException( 'WPAjaxDieStopException' );
$this->_handleAjax( 'my_plugin_get_site_pages' );
} catch ( WPAjaxDieStopException $e ) {}
//$response = json_decode( $this->_last_response );
$response = $this->_last_response;
var_dump( $response );
}
}
Now here are the issues
It doesn't throw WPAjaxDieStopException exception like its suppose to
when I do this code $this->setExpectedException( 'WPAjaxDieStopException' );
it fails the test https://snag.gy/JSTqHV.jpg
It prints out that wp_die() has been triggered, so this code
$response = $this->_last_response;
var_dump( $response );
prints this
https://snag.gy/pKqfUk.jpg
Number 2 is an issue because you cannot do json_decode the string outputted coz its an invalid json string, so I can't continue with my test.
I'm just starting out with automated testing on wordpress plugins and I appreciate any help.
Note:
My ajax callback is working ok on my live plugin even if I use wp_die(), it just prints that weird 'wp_die called ...' string on my test.
My php version is 5.6.21 and my phpunit version is 4.8.26
Here are some additional info
So both 'WPAjaxDieStopException' and 'WPAjaxDieContinueException' are not thrown,
however what's interesting is when I do this
$this->_setRole( 'administrator' );
I get this error on the console
Trying to get property of non-object
/tmp/wordpress-tests-lib/includes/testcase-ajax.php:151
/vagrant/www/wordpress/wp-content/plugins/my-plugin/tests/test-file.php:30
But clearly I'm extending WP_Ajax_UnitTestCase and it has the _setRole method
https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/testcase-ajax.php#L168
Also when I run phpunit I get this bunch of errors or warnings on the console
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
WordPress database error Duplicate key name 'location_type_code' for query ALTER TABLE wptests_woocommerce_tax_rate_locations ADD KEY location_type_code (location_type(40),location_code(90)) made by PHPUnit_TextUI_Command::main, PHPUnit_TextUI_Command->run, PHPUnit_TextUI_Command->handleArguments, PHPUnit_TextUI_Command->handleBootstrap, PHPUnit_Util_Fileloader::checkAndLoad, PHPUnit_Util_Fileloader::load, include_once('/vagrant/www/wordpress/wp-content/plugins/my-plugin/tests/bootstrap.php'), require('/tmp/wordpress-tests-lib/includes/bootstrap.php'), require_once('wp-settings.php'), do_action('init'), call_user_func_array, WC_Install::check_version, WC_Install::install, WC_Install::create_tables, dbDelta
Also I am using vagrant and use http://vccw.cc/ for my dev env and also following this guide on adding tests for woocommerce extensions
https://github.com/Automattic/wc-extensions-code-test-guide
Hope all this additional info will help in finally solving this issue.
Been away for a while, been very busy, finally got some time to figure this out. It turns out it's a stupid mistake (facepalm).
Since we are using WP_Ajax_UnitTestCase which extends WP_UnitTestCase
Then when we use function setup in WP_Ajax_UnitTestCase then we need to call this parent::setup();
public function setup() {
parent::setup();
// your init codes here
}
I was not calling that on my existing code that's why the test is acting weird. Adding that solves all the weird issues and runs the test as expected and throws necessary exceptions.
WPAjaxDieStopException is thrown if ajax callback function does not yield any output.
WPAjaxDieContinueException is thrown if ajax callback yields any output.
Also make sure to use wp_die() instead of die() on your ajax callback, if you use the later, the test will halt.
I am planning to write and extensive guide to doing automated testing in WordPress plugins, I'll put the link to it here soon.
For now I hope this helps anyone.

Getting an Unexpected T_Function Error and I don

This may be a dumb question. I am getting an unexpected T_ERROR for the following code in my WordPress Functions.php file. This happens often and I review the code several times seeing no problems... So I've decided to ask someone why.
Thank you.
<?php
/*
**Remove WordPress 'w'
*/
function annointed_admin_bar_remove() {
global $wp_admin_bar;
/* Remove their stuff */
$wp_admin_bar->remove_menu('wp-logo');
}
add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0);
/*
** Remove WordPress Dashboard Widgets
*/
function remove_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
?>

Eventbrite ticket Display

Hello I need to fetch the ticket iframe from Eventbrite to a wordpress page. I purchased the ticket plugin and is
getting PHP Fatal error: Using $this when not in object context for the following Function
for the following function for inlcude line
public function displayEventBriteTicketForm() {
include( TribeEventsTemplates::getTemplateHierarchy( 'ticket-form', 'hooks', 'eventbrite', $this->pluginPath ) );
include( TribeEventsTemplates::getTemplateHierarchy( 'ticket-form', 'modules', 'eventbrite', $this->pluginPath ) );
}
Help
Solved the Problem
replaced $this->pluginPath with $pluginPath resolved the error message

Categories