Echo variable from frontend in widget dashboard form - php

I create a WordPress widget and want to echo $widget_id variable inside
function form($instance);
to be visible inside WP dashboard. Variable exists https://codex.wordpress.org/Function_Reference/wp_add_dashboard_widget and is echoed in frontend using
function widget( $args, $instance )
without issues. Is that possible? (when I try to echo variable I get notice that variable is undefined, and it is not displayed)

function my_function( $widget, $return, $instance ){
echo $widget->id;
}
add_action( 'in_widget_form', 'my_function', 1, 3 );

Related

Disable Jetpack Carousel on specific pages in WordPress

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

Wordpress get post id

I'm creating a plugin OOP for wordpress. The plugin creates a new custom post type called teams. Within a team page a could use the shortcode [program] to generate some predefault html code. Also i've created custom fields with new meta boxes.
The problem however is: when i'm entering the page thats calling the plugin, equal the team page with the sortcode i need to get post id within my plugin to retrieve get_post_meta().
I've tried the following things:
public function __construct(){
// not working
$post;
$post->ID;
// not working
global $wp_query;
$post_id = $wp_query->post->ID;
$post = get_post( $post_id );
// not workiing
echo '<pre>';
print_r('post_id:' . get_the_ID());
echo '</pre>';
}
How could i receive the custom post id within my plugin when i visited the page from frontend (so the plugin is called, runs the shortcode)
My main class gets loaded like this:
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
run_plugin();
Within MyPlugin the constructor looks like
public function __construct() {
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
$this->version = PLUGIN_NAME_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'MyPlugin';
if(!empty(get_option($this->plugin_name))){
$this->clientID = get_option($this->plugin_name)['client_id'];
}
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_shortcodes();
}
If your plugin constructor is getting called too early, the post data won't be set up and ready to use.
You'll need to hook into one of WPs actions that run after everything is ready. The init action should be enough for the post data, but depending on what else you need you can hook into wp_loaded, as it doesn't run until after WordPress is fully loaded.
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
/* run_plugin(); */ // <- instead of this....
add_action( 'wp_loaded','run_plugin' ); // <- ...do this
Try to define post as global like
global $post

Echo PHP variable using shortcode in WordPress

I want to echo PHP variable value on any page using shortcode in WordPress.
I have created:
function custom_shortcode() {
echo $unique;
}
add_shortcode( 'test', 'custom_shortcode' );
When I place shortcode [test] on any page, it returns nothing.
The $unique variable is to show the logged-in user's username in URL. For example: example.com/?username
The expected output on front-end need to be: example.com/?username
try this:
function custom_shortcode( $atts, $content = null ) {
global $unique; // if $unique is global var add this line too
return $unique;
}
add_shortcode( 'test', 'custom_shortcode' );

global post variable not working in wordpress plugin

I understand that the Wordpress global $post may not work until a certain point because it hasnt been loaded, but Im wondering is there a workaround for this.
Basically Im building a plugin specifically for a client. At the moment the variable is showing nothing.
This code is in a plugin in the plugins folder.
Im looking to make sure it only loads javascript (Ive left out that bit) when on specific pages (selected by the user), at the moment its loading on all pages.
My other option is to do it all on a php template, but to be honest I wanted to write it as a plugin with a view to customizing it for more generic use in the future, plus I have little experience with plugins so Im trying to improve that side of things also.
function include_js() {
global $post;
print_r($post);
if(is_object($post) && $post->ID == 14 ){
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'init', 'include_js' );
EDIT: I realised my main issue is that I want to include the javascript and because of that I need wp_enqueue_script , but I can only seem to get that work if you use the init action, which happens before the loop.
After seeing your edit, try hooking to wp_enqueue_scripts instead of init. Like this:
function include_js() {
global $post;
print_r( $post );
if ( is_object( $post ) && $post->ID == 14 ) {
// do stuff
wp_enqueue_script( 'include-map', plugin_dir_url( __FILE__ ) . 'map.js' );
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
Ref: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
You should test $wp_query->queried_object_id instead of $post->id:
function include_js() {
global $wp_query;
if ( $wp_query->queried_object_id == 14 ) {
// do stuff
//...
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
There is no need for any globals etc. You can simply use is_single($post) to check if you are on a specific single post and then enqueue your script. You should always always use the wp_enqueue_scripts hook to hook your function to to enqueue scripts and styles
function include_js() {
if(is_single(14)) {
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'wp_enqueue_scripts', 'include_js', 999 );
Remeber also, always add priority when adding custom scripts/styles. See add_action( $hook, $function_to_add, $priority, $accepted_args )

Wordpress Plugin

I'm trying to use a widget within a plugin in wordpress and I'm seeing this error within the widget box:
Warning: extract() [function.extract]: First argument should be an array in /nfs/c03/h04/mnt/57957/domains/rab.qbessi.com/html/wp-content/plugins/register-plus/dash_widget.php on line 24
This is the code from Line 24:
// Output the widget contents
function widget( $args ) {
extract( $args, EXTR_SKIP );
Here's the dash_widget.php code
<?php
if( !class_exists('RegisterPlusWidget') ){
class RegisterPlusWidget{
function RegisterPlusWidget() { //contructor
// Add the widget to the dashboard
add_action( 'wp_dashboard_setup', array($this, 'register_widget') );
add_filter( 'wp_dashboard_widgets', array($this, 'add_widget') );
}
function register_widget() {
wp_register_sidebar_widget( 'regplus_invite_tracking', __( 'Invitation Code Tracking', 'regplus' ), array($this, 'widget'), array( 'settings' => 'options-general.php?page=register-plus' ) );
}
// Modifies the array of dashboard widgets and adds this plugin's
function add_widget( $widgets ) {
global $wp_registered_widgets;
if ( !isset($wp_registered_widgets['regplus_invite_tracking']) ) return $widgets;
array_splice( $widgets, 2, 0, 'regplus_invite_tracking' );
return $widgets;
}
// Output the widget contents
function widget( $args ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
echo $before_title;
echo $widget_name;
echo $after_title;
global $wpdb;
$regplus = get_option( 'register_plus' );
$codes = $regplus['codepass'];
$usercodes = array();
foreach($codes as $code){
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key='invite_code' AND meta_value='$code'" );
echo '<h3>' . $code . ': <small style="font-weight:normal">' . count($users) . ' Users Registered.</small></h3>';
}
echo $after_widget;
}
}
} # End Class RegisterPlusWidget
// Start this plugin once all other plugins are fully loaded
add_action( 'plugins_loaded', create_function( '', 'global $regplus_widget; $regplus_widget = new RegisterPlusWidget();' ) );
?>
The widget() function is being called with no parameters. Why, is hard to tell without digging deeply into the plugin. You should ask the plugin's author.
You can try adding
// Output the widget contents
function widget( $args ) {
if (is_array($args)) // Add this
extract( $args, EXTR_SKIP );
and see whether the output still makes sense then. That effectively just suppresses the action that causes the warning. If it's a badly programmed plugin that was developed with warnings turned off, that already may do the trick.
google launched new plugin called site kit, it will make analyzing work much more easier.
https://wordifact.com/google-site-kit/

Categories