Problems with add_action - php

I'm trying to create a plugin in wordpress and want to include a JS script in the tags area only when i'm viewing the menu page.
add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page() {
add_menu_page('Home', 'PCPAL', 'manage_options', 'pcpalmain', 'da_controller', '', 99);
}
function DA_controller()
{
add_action('admin_head', 'da_admin_head');
}
function da_admin_head()
{
echo "<script type='text/javascript' src='".plugins_url('js/pcpal.js', __FILE__)."'></script>";
}
This script work if i move the add_action('admin_head', 'da_admin_head'); outside the DA_function function.

you are including JS the wrong way you need to enqueue it -> http://codex.wordpress.org/Function_Reference/wp_enqueue_script
your first add_action goes to the admin_head you need to use admin_init
tutorial about enqueue-ing
http://halfelf.org/2012/jquery-why-u-no-enqueued/

Your Code has a lot off issues.... Let me help you to correct it....
Here is your code
1. add_action('admin_menu', 'register_custom_menu_page');
2. function register_custom_menu_page() {
3. add_menu_page('Home', 'PCPAL', 'manage_options', 'pcpalmain', 'da_controller', '', 4. 99);
5. }
6. function DA_controller()
7. {
8. add_action('admin_head', 'da_admin_head');
9. }
10. function da_admin_head()
{
11. echo "<script type='text/javascript' src='".plugins_url('js/pcpal.js', __FILE__)."'></script>";
}
I have added some numbering so that I can point you where is the problems....
On line 1, you added a function to admin_menu hook. ON line 3, you added a function named da_controller for creating a admin menue.
But one number 6 you just misspelled it.
Well, I do not have enough time for today... But this edited codes must work for now....
add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page() {
add_menu_page('Home', 'PCPAL', 'manage_options', 'pcpalmain', 'da_controller', '', 99);
}
function da_controller()
{
wp_enqueue_script( 'pcpal', 'js_url', array(), '1.0.0', false );
}

Related

Add some content to woocommerce product single page via a plugin

I'm somewhat new to developing plugins for Wordpress and I have this idea for a simple woocommerce based plugin. I need this plugin to do something only on single product pages but I can't see to figure out at all how I would add an action when a product gets loaded to do something.
Does anyone have any experience with this?
In my plugin I've attempted to just get this working with some code
public function get_product_variation_data() {
function get_data() {
echo '<script>';
echo "console.log('beep');";
echo '</script>';
}
add_action( 'woocommerce_after_single_product', 'get_data' );
}
But this doesn't seem to run at all on my product page. Maybe there is a different hook I need to attach to?
Try this :
class PluginTest {
public function __construct(){
add_action( 'woocommerce_after_single_product', array($this, 'get_product_variation_data'));
}
public function get_product_variation_data(){
echo '<script>';
echo "console.log('beep');";
echo '</script>';
}
}
$plugin_test = new PluginTest();
You need to add in your plugin constructor __construct() the following:
add_action( 'woocommerce_after_single_product', array( $this, 'get_product_variation_data' ), 5 );
Then outside the constructor:
public function get_product_variation_data() {
?>
<script> console.log('beep'); </script>
<?php
}
This should work…

WordPress remove_filter() to remove theme JS hook via plugin

I have a theme that is adding a custom, full-screen background image via jQuery. The theme is doing this via a class object called td_background. Within the class is a function called wp_head_hook(), and within this hook, a filter is being added for the custom bg. It looks something like this:
class td_background {
// ..some stuff
function __construct() {
add_action('wp_head', array($this, 'wp_head_hook'), 10);
}
function wp_head_hook() {
add_filter( 'td_js_buffer_footer_render', array($this, 'add_js_hook'));
}
function add_js_hook($js) {
// Custom JS added here for background image
return $js
}
}
new td_background();
I'm now trying to de-register the add_js_hook in a custom plugin I'm writing, but am having trouble wrapping my mind around how to do it with all this nesting. I've tried a few things, such as:
<?php
// This...
remove_filter( 'wp_footer', array($td_background, 'td_js_buffer_footer_render'));
// ...and this
remove_filter( 'wp_footer', 'td_js_buffer_footer_render');
// ...and even
remove_filter( 'wp_footer', 'add_js_hook', 100);
?>
I've also tried changing the above to wp_head.
Thoughts? My end goal is to de-register this JavaScript in the footer, so that I can add my own in place of it.
As it's being instantiated anonymously, we have to use the handy function remove_anonymous_object_filter() from WPSE, it would be something like:
// Run this from a plugin
add_action( 'template_redirect', 'kill_anonymous_example', 0 );
function kill_anonymous_example() {
remove_anonymous_object_filter(
'wp_head',
'td_background',
'wp_head_hook'
);
}
I tested killing wp_head as I don't have a td_js_buffer_footer_render running.

How to define and use helper functions in theme functions.php?

How to get following to work properly in Wordress Theme functions.php file?
I haven't figured out how to make the top function available to the bottom function within the theme's functions.php file. I'm not grasping how to setup hooks so they can work together. Thank you.
Filter/helper/whateveryoucallit function:
function poohToPee( $pooh_log )
{
switch( $pooh_log )
{
case 'gross-poop':
$pee_equivalent = 'Grossest of Pees';
break;
case 'ok-poop':
$pee_equivalent = 'Bland Snack Pee';
break;
case 'shang-tsung-plop':
$pee-equivalent = 'Random U-Stream';
break;
}
return $pee_equivalent;
}
Ajax handler function:
function screw_loose()
{
if( isset($_REQUEST['pooh_log']) )
{
echo poohToPee( $_REQUEST['pooh_log'] );
}
}
add_action('wp_ajax_priv_screw_loose', 'screw_loose')
The add_action usually calls the function you are passing through at the point that hook is called.
Since you are using some sort of ajax hook are you really able to make sure your function isn't being called? It wouldn't be echo-ing anything out to the screen since it is running in the background.
Normally any function you define in functions.php is readily available to use within the theme.
It is obviously normally best to organize and have classes, in which case you'd pass the method to the hook as an array, for example add_action( 'admin_init', array( $this, 'someFunction' ) ); and that add_action I just did would be put in the __construct function of the class.
For instance you could do this:
class helloWorld
{
function __construct()
{
add_action( 'admin_init', array( $this, 'echoItOut' ) );
}
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
Alternatively you could also do this:
class helloWorld
{
function echoItOut()
{
echo 'Hello World';
}
}
$helloWorld = new helloWorld;
add_action( 'admin_init', array( $helloWorld, 'echoItOut' ) );
Or simply:
function echoItOut()
{
echo 'Hello World';
}
add_action( 'admin_init', 'echoItOut' );
If you put any of these blocks of code I provided in your functions.php file and go to your Dashboard you will see 'Hello World' printed out at the top under the admin bar most likely (might vary from theme to theme if the dashboard has custom styling).

Run JS only on pages with my widget

I'm trying to get a widget to only load javascript on a page where the widget is present.
I've tried adding the add action in the 'showWidget' didn't work.
What am I doing wrong?
PHP
wp_register_sidebar_widget('MyWidget','MyWidget', 'showWidget');
add_action('wp_enqueue_scripts', 'addScript'); //now the script appears on every page
function addScript()
{
wp_register_script('MyWidgetJs', plugins_url( '/script.js' , __FILE__), array('jquery'));
wp_enqueue_script('MyWidgetJs');
}
function showWidget($args)
{
// add_action('wp_enqueue_scripts', 'addScript'); //I tried this but it doesn't work :(
wp_enqueue_script('MyWidgetJs');
extract($args);
/* do widget stuff */
}
Seems that the new version of WordPress supports this: http://codex.wordpress.org/Version_3.3
Register your script, but don't enqueue it. In your widget PHP, add in the wp_enqueue_script('your_script_name'); and it'll load it only when your widget is used and place it in the footer.
You are going to upgrade to 3.3, right? :)
SO, something like this should work just fine:
wp_register_sidebar_widget('MyWidget','MyWidget', 'showWidget');
function showWidget($args) {
wp_enqueue_script('MyWidgetJs');
extract($args);
/* do widget stuff */
}
You want to use is_active_widget() conditional to do something like:
<?php
if ( is_active_widget('MyWidget') ) {
add_action('wp_enqueue_scripts', 'addScript');
}
?>
using your above code the final widget may look like:
<?php
if ( is_active_widget('MyWidget') ) {
add_action('wp_enqueue_scripts', 'addScript');
}
function addScript()
{
wp_register_script('MyWidgetJs', plugins_url( '/script.js' , __FILE__), array('jquery'));
wp_enqueue_script('MyWidgetJs');
}
function showWidget($args)
{
wp_enqueue_script('MyWidgetJs');
extract($args);
/* do widget stuff */
}

wordpress plugin : Call to undefined function add_menu_page()

I am working on a wordpress plugin and i decided to use OOP instead of functional programming,however;I am receiving this weird error:
Error Message:
Call to undefined function add_menu_page() while am pretty sure everything is working as intended
The Code:
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
and i initiate the code with $admin = new bdmin();
Best Regards
This should remove the error:
<?php
if(!empty($value['id'])) {
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
}
?>
To solve this problem all I needed to do is invoke a function named add_action with these parameters.
add_action('admin_menu', array( &$this , 'create_admin_menu'));
Make sure you include the header file at the top so you can make WP function calls
<?php require('{correct_path}/wp-blog-header.php');

Categories