so i'm developing my first WordPress plugin and i am having some difficulties...
I am doing it Object Oriented...
In the bottom, when 'plugins_loaded', i Create a new instance of myClass. It also enques a javascript, everytime any page is loaded. This script registration works, because i get a console.log every page load. It then registers an action on 'publish_post' that is fired when an admin publishes(saves) a new post and invokes my publish_post() method.
The method is called, when a post is published; i know it because if i uncomment it's two first lines, the sctipt dies with my var_dump.
My problem is that wp_enque_script() is not working in this method. For some reason my script isn't called...
Here's the code:
<?php
class myClass{
function __construct(){
// hooks & filters..
add_action( 'publish_post', array($this, 'publish_post'));
wp_enqueue_script(
'plugin', //$handle
plugins_url('/js/plugin.js', __FILE__)//$src
);
}
function publish_post(){
//global $wp_query;
//die(var_dump($wp_query));
wp_enqueue_script(
'publish', //$handle
plugins_url('/js/publish.js', __FILE__)//$src
);
}
}
/* Initialise outselves */
add_action( 'plugins_loaded', create_function( '', 'global $myObject; $myObject = new myClass;' ));
?>
Anyone has any idea why this is happening? thanx
Just had the same problem. You have to add it to a hook, for example the init (I tried with admin_head hook but that didn't work so I picked init because I saw it in another plugin. And it seem to work fine for me)
In you construct add:
add_action('init', array($this, 'loadMyScripts'));
and in the function called by the action:
public function loadMyScripts()
{
wp_enqueue_script(
'publish', //$handle
plugins_url('/js/publish.js', __FILE__)//$src
);
}
Related
I recently updated my website to WP 5.2.2 and 'init' action seems not to fire anymore.. I had few VC element mapped to that action but they are not working anymore..
This is the vc_map init code:
class VC_Extensions_FancyBox extends WPBakeryShortCode {
function __construct() {
if( has_action('init') ){
die('has init'); // this is printing correctly
}
add_action( 'init', array($this, 'banner_init'));
add_shortcode('vc_fancybox', array($this, 'vc_fancybox_func'));
}
function banner_init() {
if( has_action('init') ){
die('has banner_init'); //this is not printing at all..
}
vc_map( array(........) );
}
function vc_fancybox_func() {
....
}
}
I added 2 checks in the code, the first one debugs correctly, the other one doesn't. Any idea why this is happening?
Thank you very much
EDIT: using action 'wp_loaded' the element is showing correctly...
As per Edit, I just replace 'init' to 'wp_loaded' and Worked perfectly
add_action( 'wp_loaded', array( $this, 'vc_progressbar_mapping' ) );
//add_action( 'init', array( $this, 'vc_progressbar_mapping' ) );
Please consider the code:
function mcqac_wp_enqueue_assets() {
if (is_admin()) {
wp_enqueue_script(
'mcqac-js-admin', // Handle.
PLUGIN_URL . 'build/main-admin.js',
array( 'jquery' ), // Dependencies, defined above.
filemtime( PLUGIN_PATH . 'build/main-admin.js' ), // Version: File modification time.
true // Enqueue the script in the footer.
);
$mcqacAdminData = array();
if (get_the_ID()) {
$mcqacAdminData['options'] = get_post_meta(get_the_ID(), 'mcqac_options', true);
}
wp_localize_script('mcqac-js-admin', 'mcqacAdminData', $mcqacAdminData);
}
}
add_action('init', 'mcqac_wp_enqueue_assets');
The get_the_ID() does not return anything when I am in the edit post page. Seems like init action hook is fired before the post query.
What is the solution?
Problem fixed the admin_enqueue_scripts action hook instead of init action hook. And also declare global $post to get post id from this variable.
Try the template_redirect hook instead of init.
add_action('template_redirect', 'prefix_get_page_id');
I'm using the WordPress plugin Ultimate member which has a hook called um_user_register which should happen after a user fills out a registration form. According to the documentation (https://docs.ultimatemember.com/article/1308-umuserregister) I should be able to use that hook and do my thing. Unfortunately nothing happens.
The code I have added to my theme's functions.php file:
//Ultimate Member - Extra stuff after user registers
add_action( 'um_user_register', 'my_user_register', 10, 2 );
function my_user_register( $user_id, $args ) {
add_user_meta( $user_id, 'user_utlimate_member_signup', 'Yes');
}
Am I putting my add_action or function in the wrong place or is there something I'm overlooking?
I am embedding my Jplayer code into my website however there is already jquery within the wordpress installation but when I copy my code across the player is broken as it shows no the volume icon with the x over it. When I include an external jquery script it breaks the entrie site but the player works. Is there any better way for me to include jPlayer into my wordpress site?
The jPlayer script enqueueing has to play by WordPress rules. The easiest (and maybe only) way is with a Shortcode. There are lot of developers, mainly Theme developers, that ignore that we don't dequeue the bundled jQuery version and load any version from some CDN (at least, we don't do it without knowing exactly what we're doing).
Here's a rough test, the shortcode callback function has to be polished a lot.
public function plugin_setup() // hooked into plugins_loaded
{
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
add_shortcode( 'jplayer', array( $this, 'shortcode' ) );
}
public function enqueue()
{
wp_register_script(
'sj-jplayer',
$this->plugin_url . 'js/jquery.jplayer.min.js',
array( 'jquery' ), // <------- Dependencies
false,
true
);
wp_register_style( 'sj-skin', $this->plugin_url . 'skin/blue.monday/jplayer.blue.monday.css' );
wp_enqueue_script( 'sj-jplayer' );
wp_enqueue_style( 'sj-skin' );
}
public function shortcode( $atts, $content )
{
ob_start();
require_once('html-shortcode.php');
$var = ob_get_clean();
return $var;
}
The file html-shortcode is basically this demo code adapted like:
<?php
/*
* Prints the shortcode
*/
?>
<script type="text/javascript">
jQuery(document).ready(function($) // <------ WP noConflict
{
$("#jquery_jplayer_1").jPlayer({});
});
</script>
<div id="jquery_jplayer_1" class="jp-jplayer"></div>
I tested this jPlayer shortcode inside another that does a jScrollPane, and it worked on the iPad.
What I want is to add my custom javascript to my plugin's home page only.
Here is what I did:
1. First registered my script,
function register_my_plugin_script() {
/* Register our script. */
wp_register_script( 'my-plugin-script', plugins_url('/script.js', __FILE__), array('jquery'));
}
add_action( 'admin_init', 'register_my_plugin_script' );
2. Then made a call to it,
function call_my_script(){
wp_enqueue_script( 'my-plugin-script' );
}
3. Then tried to call it only in my home page like this:
function my_home_page(){
add_action('admin_enqueue_scripts', 'call_my_script');
}
But it did not add the script.
Further code of adding menu page :
function my_menu_pages(){
add_menu_page('some title', 'some menu', 'manage_options', 'my-plugin-homepage', 'my_home_page');
}
add_action('admin_menu', 'my_menu_pages');
Note:
if I include my script directly in my_home_page function without registering or calling like this, then it works.
function my_home_page(){
wp_enqueue_script( 'my-plugin-script', plugins_url('/script.js', __FILE__), array('jquery'));
}
What is the best way to do this?
There is a hook for your plugin's pages in the admin area - but it can be difficult to work out exactly what it is called.
Try this first:
add_action('admin_enqueue_scripts', 'my_plugin_enqueue_scripts' );
function my_plugin_enqueue_scripts($hook)
{
echo "<!-- Hook = {$hook} -->" . PHP_EOL;
}
... then visit your plugin's page in the admin area and view the HTML source to find the output of that variable. Alternatively, set a breakpoint and view it in your debugger.
Once you've found the hook name, you can do the following in your plugin code:
add_action('admin_enqueue_scripts', 'my_plugin_enqueue_scripts' );
function my_plugin_enqueue_scripts($hook)
{
if ($hook == 'my_plugin_page_hook-location')
{
wp_register_script(...);
wp_enqueue_script(...);
wp_register_style(...);
wp_enqueue_style(...);
}
}