I'm new to jquery and php and trying to create my first Wordpress Plugin.
The plugin simply adds a metabox to posts which has a date field on the backend.
Metabox and field all added ok and appear when I go to create or edit a post.
However, I can't get the datepicker to work. Nothing happens when you click on the input box, the date selector does not show. Although the input box will only let me input numbers not letters.
The plugin is a single page of php code.
The top of my plugin file attempts to load the jquery:
function ca_load_jquery_datepicker() {
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css');
}
add_action( 'admin_enqueue_scripts', 'ca_load_jquery_datepicker' );
Then this is the input and jquery in my form (lower down on the plugin file):
<input type="text" class="date" name="ca_expiry_date" value="">
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.date').datepicker({
dateFormat : 'yy-mm-dd'
});
});
</script>
Can anyone help advise where I am going wrong with the datepicker?
Thank you!
UPDATE
Thanks for the comments, I have discovered that if I instal the "Classic Editor" plugin, my code works. But when that is not activated and using Gutenberg it does not work.
The error is (without sharing the site url):
GET https://xxxx.co.uk/wp-content/themes/publisher/gutenberg-7.6.2.min.css?ver=7.6.2 net::ERR_ABORTED 404 (Not Found)
This error is still there even when my plugin is disabled.
However, if I activate the Twenty Nineteen theme there is no error but the date field in my plugin still does not work.
So clearly gutenberg does not like something in my code...
I would make sure to register the whole jquery-ui package from the remote official source in the functions.php of your current theme, like this:
function ca_load_jquery_ui() {
// first, register the style from the remote official source
wp_register_style('jqueryuicss', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.min.css', array('jquery-ui-styles'), '1.12.1');
wp_enqueue_style('jqueryuicss');
// then, register the core file from the remote official source, in footer
wp_register_script('jqueryui', '//code.jquery.com/ui/1.12.1/jquery-ui.min.js', array('jquery-ui'), '1.12.1', true);
wp_enqueue_script('jqueryui');
}
add_action( 'wp_enqueue_scripts', 'ca_load_jquery_ui' );
Note: if you want to use your calendar in the frontend, please make sure to use the function add_action( 'wp_enqueue_scripts', 'xxx' ); instead of add_action( 'admin_enqueue_scripts', 'xxx' ); which is only for the admin side.
UPDATE
You might also try to disable the default jQuery or jQuery-ui to see if that helps:
//remove the default jQuery script
function remove_default_jquery(&$scripts){
if(is_admin()){
// try this...
$scripts->remove('jquery');
// or...
$scripts->remove('jquery-ui');
}
}
add_filter( 'wp_default_scripts', 'remove_default_jquery' );
Related
The Mega Menu plugin works well except when activated it affects But some WordPress Dash icons not showing properly.
I have already installed cache plugins like Autoptimize and WP Rocket as well.
After clear cache from admin side but don't work till now. Also not giving any error in the Browser console.
URL: https://www.socomtactical.net
Max Mega Menu Plugin: https://wordpress.org/plugins/megamenu/
Note: I can't use Elemetor on my Website.
Screenshot:
Please let me know what is actually issue?
Thank you.
If you want to add Dashicons on WordPress frontend, you will need to enqueue them using PHP code in your theme functions.php file:
/* Add Dashicons in WordPress Front-end */
add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );
function load_dashicons_front_end() {
wp_enqueue_style( 'dashicons' );
}
Sample :
<i class="dashicons dashicons-admin-comments"></i>
Chose icon
I've created a child theme and am running my site on that so that I can customise a form that I'm including on certain pages using the plugin 'contact-form-7". In my child folder I've placed a style.css, functions.php and js/custom_script.js. the style sheet is being imported fine but I can't seem to get my validation jquery file working on that form. This is the code I've been using:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_register_script( 'custom_script', get_stylesheet_directory_uri() . '/js/custom_script.js' ,array( 'jquery'));
wp_enqueue_script( 'custom_script' );
}
console isn't logging any errors but I can't seem to find it in the web tools side bar UNLESS I remove "js/" when the custom_script is called, in which case a GET error pops up.
I'm guessing I'm incorrectly importing the file and honestly I can't quite get my head around these hooks and importing files just yet- only been using wordpress a short time.
Any help much appreciated.
I believe that you haven't included the JQuery file? You have to download it first https://jquery.com/download/, and then register it as you did with "custom_script", you just have to put it before the "custom_script" in functions.php, inside "custom_script.js" you could also do:
$(document).ready(function(){
});
Go to your console and if you see:
Uncaught ReferenceError: $ is not defined
It's because you haven't included the JQuery file as needed.
Hope this helps!
Leo.
I am developing a plugin in WordPress and for the same plugin in settings page I want to integrate a jquery in backend on edit.php. I have followed the below method but it is not working
function cs_colorpicker_js() {
wp_enqueue_script('cs_colorpicker_js',
plugin_dir_url(__FILE__).'js/cs_colorpicker.js'
);
}
add_action( 'admin_enqueue_scripts', 'cs_colorpicker_js' );
here is the screenshot of one error that i have found. http://goo.gl/wmgH60
Use the third variable of Wp_enqueue_script() like,
function cs_colorpicker_js() {
wp_enqueue_script('cs_colorpicker_js',
plugin_dir_url(__FILE__).'js/cs_colorpicker.js',
array('jquery') // jquery dependency
);
}
I have been thinking about developing my own theme framework for worpdress. I'd like to use jquery ui to build a bootstrap 3.0 drag and drop interface, which I already have worked out, but I can't figure out how to edit the "Pages Add New Screen" as referenced here: https://codex.wordpress.org/Pages_Add_New_Screen
Would I add files to my client side theme that affected my admin structure as well? Does anyone have any suggestions as to how to do something like this. Alot of themes these days come with these drag and drop frameworks and it would be nice, to be able to create one of my own, just need some direction on where to start editing / looking.
We add a Custom Meta Box and do our thing inside it.
There are some hooks that are not Meta Boxes and we can use to insert content into that admin page:
add_action( 'edit_form_top', function( $post )
{
echo '<h1 style="color:red">edit_form_top</h1>';
});
add_action( 'edit_form_after_title', function( $post )
{
echo '<h1 style="color:red">edit_form_after_title</h1>';
});
add_action( 'edit_form_after_editor', function( $post )
{
echo '<h1 style="color:red">edit_form_after_editor</h1>';
});
add_action( 'edit_page_form', function( $post )
{
// edit_page_form is ONLY for pages, the other is for the rest of post types
echo '<h1 style="color:red">edit_page_form/edit_form_advanced</h1>';
});
add_action( 'dbx_post_sidebar', function( $post )
{
echo '<h1 style="color:red">dbx_post_sidebar</h1>';
});
The widget_text block belongs to Advanced Custom Fields (it's a repeatable/sortable field). I'm not sure anymore, but I think it removes the Meta Box borders with CSS or jQuery.
Instead of messing with the core files of wordpress (which would be a really bad idea) I would suggest making a custom field/sidebar or a menu page to use the drag/drop function to make a page or post.
http://codex.wordpress.org/Custom_Fields
http://codex.wordpress.org/Function_Reference/add_menu_page
If you want some examples on how others have done something similar (as a plugin) you could look at the code from others and see if you can use similar techniques to add additional fields within the screen options.
http://simple-fields.com/
http://www.advancedcustomfields.com/
i created a wp plugin, now, whenever i change it through
a) editor,
b) manually in Sublime Text2 and then delete from wp, and re-upload the changed version
and go to page with shortcode that's supposed to trigger plugin, i see no changes. i now have just few words and no html tags whatsoever in php page its supposed to include, it still displays the old html table that was there
here's the code i use to activate the plugin when i 'install' it in WP
register_activation_hook( __FILE__, 'act');
function act(){
add_option('Activated_Plugin','Plugin-Slug');
/* activation code here */
}
function getxml( $atts ){
include 'getxml.php';
}
add_shortcode( 'boixml', 'getxml' );
Did you install W3 Total Cache or WP Super Cache plugins?
Try that link may it give you the key to solve that problem
http://codex.wordpress.org/WordPress_Optimization/Caching