I am writing a custom WordPress plugin, I am using the OceanWP Theme with Elementor, and am trying to enqueue/register Bootstrap 4.5 styles/scripts as well as my own custom styles/scripts.
However, OceanWP's styles are still taking precedence being used instead of my styles/scripts.
Currently, I am trying to over ride the themes assets by upping the priority in the add_action hook but am not having any luck.
I am trying to display a custom multi-part form and display it on a page using a shortcode.
public function __construct()
{
//set dirpath
$this->_dirpath = dirname(__FILE__);
add_action('wp_enqueue_scripts', array($this, 'cmmc_styles'), 9999);
add_action('wp_footer', array($this, 'cmmc_scripts'));
add_shortcode("sme-cmmc-form", array($this, "displayCmmcForm"));
}
public function cmmc_scripts()
{
///wp_enqueue_style('bootstrap_css', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css', false, NULL, 'all');
wp_enqueue_script('popper_js', 'https://cdn.jsdelivr.net/npm/popper.js#1.16.1/dist/umd/popper.min.js', array('jquery'), NULL, true);
wp_enqueue_script('bootstrap_js', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js', array('jquery'), NULL, true);
wp_enqueue_script('cmmc_js', plugin_dir_url( __FILE__ ) . 'assets/js/app.js', array('jquery'), '1.0' );
//wp_enqueue_style('custom_styles', plugins_url('/assets/css/styles.css', __FILE__));
}
public function cmmc_styles() {
wp_register_style('bootstrap_css', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css', false, NULL, 'all' );
wp_enqueue_style('bootstrap_css');
wp_enqueue_style('custom_styles', plugins_url('/assets/css/styles.css', __FILE__));
}
Can someone please tell me how I could possibly over ride the themes styles, even if it is just for this plugin, or dequeue the styles for this single page temporarily?
EDIT: to add in the enqueued styles and scripts from the theme
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'theme_css' ) );
// Load his file in last.
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'custom_style_css' ), 9999 );
// Remove Customizer CSS script from Front-end.
add_action( 'init', array( 'OCEANWP_Theme_Class', 'remove_customizer_custom_css' ) );
// Load theme js.
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'theme_js' ) );
/**
* Load front-end scripts
*
* #since 1.0.0
*/
public static function theme_css() {
// Define dir.
$dir = OCEANWP_CSS_DIR_URI;
$theme_version = OCEANWP_THEME_VERSION;
// Remove font awesome style from plugins.
wp_deregister_style( 'font-awesome' );
wp_deregister_style( 'fontawesome' );
// Load font awesome style.
wp_enqueue_style( 'font-awesome', OCEANWP_THEME_URI . '/assets/fonts/fontawesome/css/all.min.css', false, '5.11.2' );
// Register simple line icons style.
wp_enqueue_style( 'simple-line-icons', $dir . 'third/simple-line-icons.min.css', false, '2.4.0' );
// Register the lightbox style.
wp_enqueue_style( 'magnific-popup', $dir . 'third/magnific-popup.min.css', false, '1.0.0' );
// Register the slick style.
wp_enqueue_style( 'slick', $dir . 'third/slick.min.css', false, '1.6.0' );
// Main Style.css File.
wp_enqueue_style( 'oceanwp-style', $dir . 'style.min.css', false, $theme_version );
// Register hamburgers buttons to easily use them.
wp_register_style( 'oceanwp-hamburgers', $dir . 'third/hamburgers/hamburgers.min.css', false, $theme_version );
// Register hamburgers buttons styles.
$hamburgers = oceanwp_hamburgers_styles();
foreach ( $hamburgers as $class => $name ) {
wp_register_style( 'oceanwp-' . $class . '', $dir . 'third/hamburgers/types/' . $class . '.css', false, $theme_version );
}
// Get mobile menu icon style.
$mobileMenu = get_theme_mod( 'ocean_mobile_menu_open_hamburger', 'default' );
// Enqueue mobile menu icon style.
if ( ! empty( $mobileMenu ) && 'default' !== $mobileMenu ) {
wp_enqueue_style( 'oceanwp-hamburgers' );
wp_enqueue_style( 'oceanwp-' . $mobileMenu . '' );
}
// If Vertical header style.
if ( 'vertical' === oceanwp_header_style() ) {
wp_enqueue_style( 'oceanwp-hamburgers' );
wp_enqueue_style( 'oceanwp-spin' );
}
}
/**
* Returns all js needed for the front-end
*
* #since 1.0.0
*/
public static function theme_js() {
// Get js directory uri.
$dir = OCEANWP_JS_DIR_URI;
// Get current theme version.
$theme_version = OCEANWP_THEME_VERSION;
// Get localized array.
$localize_array = self::localize_array();
// Comment reply.
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
// Add images loaded.
wp_enqueue_script( 'imagesloaded' );
// Register nicescroll script to use it in some extensions.
wp_register_script( 'nicescroll', $dir . 'third/nicescroll.min.js', array( 'jquery' ), $theme_version, true );
// Enqueue nicescroll script if vertical header style.
if ( 'vertical' === oceanwp_header_style() ) {
wp_enqueue_script( 'nicescroll' );
}
// Register Infinite Scroll script.
wp_register_script( 'infinitescroll', $dir . 'third/infinitescroll.min.js', array( 'jquery' ), $theme_version, true );
// WooCommerce scripts.
if ( OCEANWP_WOOCOMMERCE_ACTIVE
&& 'yes' !== get_theme_mod( 'ocean_woo_remove_custom_features', 'no' ) ) {
wp_enqueue_script( 'oceanwp-woocommerce', $dir . 'third/woo/woo-scripts.min.js', array( 'jquery' ), $theme_version, true );
}
// Load the lightbox scripts.
wp_enqueue_script( 'magnific-popup', $dir . 'third/magnific-popup.min.js', array( 'jquery' ), $theme_version, true );
wp_enqueue_script( 'oceanwp-lightbox', $dir . 'third/lightbox.min.js', array( 'jquery' ), $theme_version, true );
// Load minified js.
wp_enqueue_script( 'oceanwp-main', $dir . 'main.min.js', array( 'jquery' ), $theme_version, true );
// Localize array.
wp_localize_script( 'oceanwp-main', 'oceanwpLocalize', $localize_array );
}
It's hard to say how to dequeue the scripts w/o having a look at the theme source code. Anyways usually you just need to wait until the theme has done it's job, hook into wp and them remove the styles searching for their handles name. Something like this:
add_action('after_setup_theme','alter_styles');
function alter_styles(){
wp_dequeue_style();
wp_dequeue_script();
}
Instead, speaking of your code, first question is: are you sure you load that code at the right time, in the right place or does it get executed at all?
You can do something like:
add_action('template_redirect','my_template_redirect');
function my_template_redirect(){
if (is_page('your_page')){
// Load class / do stuff with scripts/styles
}
}
to be sure and execute the code just for that particular page
If you're using the same css class names, in your css file just add !important before the semi-colon to the attributes you want to force.
There are a number of ways you can get your stylesheets to load after other stylesheets are loaded. You have already tried a few ways, but the parent theme is a very high priority of 9999 so you need to use an even higher one or it won't work.
1. Priority
You are using priority 9999 in your add_action, but if you look at the parent theme, it uses:
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'custom_style_css' ), 9999 );
The priority for your code isn't actually higher than the parent theme, so you need to go higher again, e.g.
add_action('wp_enqueue_scripts', array($this, 'cmmc_styles'), 10000);
2. Dequeuing (Note, you need to match the values used when it was enqueued)
You said dequeuing didn't work for you, but note that when you dequeue a style the priority determines when it runs - just like when you are enqueuing - so you need to use a higher priority than what was used when it was enqueued. It also must use the same hook (or a later one).
As we saw above, you need to do this with a priority higher than the 9999 they were enqueued with, e.g.
function dequeue_oceanwp_styles() {
wp_dequeue_style('oceanwp-style');
wp_deregister_style('oceanwp-style');
}
/* Now call this function with a higher priority than 9999 */
add_action( 'wp_enqueue_scripts', 'dequeue_oceanwp_styles', 10000);
3. Dependencies
If that doesn't work you can set up dependencies between the styles to force the order.
When you use either wp_register_style or wp_enqueue_style, you can specify dependencies for the stylesheet you are registering/enqueuing - i.e. the other stylesheets needed by your stylesheet. This means that the stylesheet you are registering won't get loaded until after the ones it depends on.
To do this, you pass an array of the handles for the stylesheets that must be loaded first, e.g.
// create an array with the handles of the stylesheets you want to load before yours
$dependencies = array('oceanwp-style', 'oceanwp-hamburgers', /* etc. */ );
/* Noew pass this in as the dependencies for your stylesheets */
wp_register_style('bootstrap_css',
'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css',
$dependencies, /* array of dependencies */
NULL, 'all' );
wp_enqueue_style('bootstrap_css');
/* Add bootstrap to the dependencies, if your custom_styles needs it */
$dependencies[] = 'bootstrap_css';
wp_enqueue_style('custom_styles',
plugins_url('/assets/css/styles.css', __FILE__),
$dependencies, /* array of dependencies */
);
Related
I'm successfully using the bootstrap modal package on our website (using wordpress), but the JS is loading on every page, and I only use it on 3 specific pages. I'm looking for the best way to load the JS only on the 3 specific pages in question. The site in question is: https://www.bakashana.org.
The three pages where the Modals operate are: https://www.bakashana.org/sponsor-a-young-woman, https://www.bakashana.org/grantees-with-sponsors, and https://www.bakashana.org/graduates
The Bootstrap package has two files: bootstrap.css and bootstrap.min.js. These files are placed in the css and js folders respectively in my child theme (/wp-content/themes/astra-child/css and /wp-content/themes/astra-child/js).
The Bootstrap is registered and enqueued in my functions.php file as follows:
function themeprefix_bootstrap_modals() {
wp_register_script ( 'modaljs' , get_stylesheet_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '1', true );
wp_register_style ( 'modalcss' , get_stylesheet_directory_uri() . '/css/bootstrap.css', '' , '', 'all' );
wp_enqueue_script( 'modaljs' );
wp_enqueue_style( 'modalcss' );
}
add_action( 'wp_enqueue_scripts', 'themeprefix_bootstrap_modals');
I'm a novice at JS and don't know where/how to add code to the functions.php file in order to call only for specific pages to load.
Thanks in advance!
So after some research I figured it out myself, mostly thanks to this link:
https://www.youtube.com/watch?v=Fw6VDOZYqrM
The code uses the if( is_page) as follows:
function themeprefix_bootstrap_modals() {
if( is_page( array (1071, 3725, 3814) ) ) {
wp_register_script ( 'modaljs' , get_stylesheet_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '', true );
wp_register_style ( 'modalcss' , get_stylesheet_directory_uri() . '/css/bootstrap.css', '' , '', 'all' );
wp_enqueue_script( 'modaljs' );
wp_enqueue_style( 'modalcss' );
}
}
I want to echo out the version of a particular wp_register_script or wp_enqueue_script on the frontend.
Like Superfish JS registered via theme core files:
wp_register_script( 'superfish', GENESIS_JS_URL . "/menu/superfish{$this->suffix}.js", array( 'jquery', 'hoverIntent' ), '1.7.10', true );
wp_register_script( 'superfish-args', apply_filters( 'genesis_superfish_args_url', GENESIS_JS_URL . "/menu/superfish.args{$this->suffix}.js" ), array( 'superfish' ), PARENT_THEME_VERSION, true );
And, I want to get the version of these JS reflected on frontend via:
add_filter('the_content','addToEndOfPost');
function addToEndOfPost($content) {
if (is_single()){
return $content . '<p>Version of Superfish JS:</p>';
}
return $content;}
Even if I could get the separate script as a variable, I might be able to use a delimiter to separate out the version and id of these scripts. I'm trying WordPress development for the first time, so please help.
To load a script with condition you need to use wp_enqueue_scripts action - https://developer.wordpress.org/reference/functions/wp_register_script/
add_action('wp_enqueue_scripts','my_custom_scripts');
function my_custom_scripts() {
wp_register_script( 'superfish', GENESIS_JS_URL . "/menu/superfish{$this->suffix}.js", array( 'jquery', 'hoverIntent' ), '1.7.10', true );
wp_register_script( 'superfish-args', apply_filters( 'genesis_superfish_args_url', GENESIS_JS_URL . "/menu/superfish.args{$this->suffix}.js" ), array( 'superfish' ), PARENT_THEME_VERSION, true );
//Add your condition and enqueue script
if (is_single()){
wp_enqueue_script('superfish');
}
}
I want to include bootstrap inside my WordPress installation. I know how to include scripts and styles to a theme, but this isn't what I want.
I've looked to the WordPress core files and I've tried to add the bootstrap files to the script-loader.php file but they will not be loaded. Is there any way to add these files to the WordPress core loaded styles and scripts?
for example, I've added this line to the default wordpress stylesheet/script loader function:
$styles->add( 'bootstrap', "/includes/css/bootstrap$suffix.css" );
$scripts->add( 'bootstrap', "/includes/js/bootstrap$suffix.js", array( 'jquery' ) );
but this will not work.
I would do this by altering your functions.php in your current theme.
There are a 2 functions you should know about. The first one is to add CSS.
wp_enqueue_style( $handle, $src, $deps, $ver, $media );
Documentation
The second one is to add JS
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);
Documentation
Wordpress has a basic documentation on how to do it both:
Documentation
Eventually you going to have something like this:
function add_theme_scripts() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/slider.css', array(), '1.1', 'all');
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
Good luck!
I have a responsive Wordpress theme from "Elegant Themes" that works well, but there's a page that I wish to be non-responsive. I created a style sheet that successfully renders the site non-responsive, but I only want to it to use the said style sheet when a particular page template is used.
I've determined that the logic is probably executed it the functions.php file, and that I'll likely need to use "wp_enqueue_style", but I can't seem to crack it. Here's what I have right now (my addition is below Loads the main style sheet comment:
function et_nexus_load_scripts_styles(){
global $wp_styles;
$template_dir = get_template_directory_uri();
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
wp_enqueue_script( 'superfish', $template_dir . '/js/superfish.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'nexus-custom-script', $template_dir . '/js/custom.js', array( 'jquery' ), '1.0', true );
wp_localize_script( 'nexus-custom-script', 'et_custom', array(
'mobile_nav_text' => esc_html__( 'Navigation Menu', 'Nexus' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'et_hb_nonce' => wp_create_nonce( 'et_hb_nonce' ),
) );
$et_gf_enqueue_fonts = array();
$et_gf_heading_font = sanitize_text_field( et_get_option( 'heading_font', 'none' ) );
$et_gf_body_font = sanitize_text_field( et_get_option( 'body_font', 'none' ) );
if ( 'none' != $et_gf_heading_font ) $et_gf_enqueue_fonts[] = $et_gf_heading_font;
if ( 'none' != $et_gf_body_font ) $et_gf_enqueue_fonts[] = $et_gf_body_font;
if ( ! empty( $et_gf_enqueue_fonts ) ) et_gf_enqueue_fonts( $et_gf_enqueue_fonts );
/*
* Loads the main stylesheet.
*/
if ( is_page_template('custom-pagetemplate.php') ) {
wp_register_style( ‘custom-style’, get_stylesheet_directory_uri() . ‘/customstyles.css’ );
wp_enqueue_style( ‘custom-style’ );
} else {
wp_enqueue_style( 'nexus-style', get_stylesheet_uri() );
}
}
add_action( 'wp_enqueue_scripts', 'et_nexus_load_scripts_styles' );
From what I can tell, I have my condition set up correctly using is_page_template, but it definitely isn't using the right style sheet to load the page.
Note: I posted this question to Elegant Themes' customization support board, and the moderator said that what I'm trying to do is possible, but it would necessitate a level of customization that would require me to hire a third party developer.
Having said that, if I'm missing something huge here and need to pick up a PHP book (I know very little about PHP), or shell out some cash to hire someone to get this done, please don't hesitate to set me straight. Otherwise, any input is much appreciated.
Thanks!
Sorry to jump the gun and answer my own question to quickly, but I got it working using the following code:
if ( is_page_template('custom-pagetemplate.php') ) {
wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/customstyle.css' );
} else {
wp_enqueue_style( 'nexus-style', get_stylesheet_uri() );
}
}
add_action( 'wp_enqueue_scripts', 'et_nexus_load_scripts_styles' );
My primary issue was that I was using get_stylesheet_directory_uri() instead of get_template_directory_uri.
This article helped me out a lot: How to enqueue a custom stylesheet via functions.php in WordPress
Thanks!
You can do something similar to this:
if(is_page($page)) {
// Load non responsive css
}
else {
// load normal css
}
http://codex.wordpress.org/Function_Reference/is_page
I have a query to get a post in wordpress:
<?php
$page_content = get_page(2);
echo do_shortcode($page_content->post_content);
?>
The page that is being loaded has a shortcode in it that loads a slider. The slider markup is loading but the javascript and css files required for the plugin are not showing up in the source code. How can I fix this?
you did not supply any relative source code or link, but did you load the JS and css ?
Is the slider comes from a plugin ? is it your plugin ? is the plugin activated for said page ? did you enqueue your styles and scripts ??
Anyhow, as a first measure , try to load it manually on the page
function my_scripts_enq()
{
// Register the script for a plugin:
wp_register_script( 'custom-script', plugins_url( '/js/custom-script.js', __FILE__ ) );
// or
// Register the script for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.js' );
// then enqueue the script:
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_enq' );
For fast debug you can can simply try :
if( is_page(42) ){
add_action( 'wp_enqueue_scripts', 'my_scripts_enq' );
}
for styles you should do :
function load_styles()
{
// Register the style for a plugin:
wp_register_style( 'custom-style', plugins_url( '/css/custom-style.css', __FILE__ ), array(), '20120208', 'all' );
// or
// Register the style for a theme:
wp_register_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), 'se16205117', 'all' );
// then enqueue the style:
wp_enqueue_style( 'custom-style' );
}
add_action( 'wp_enqueue_scripts', 'load_styles' );
BTW - as a side note , From codex
$page_id = 2;
$page_data = get_page( $page_id );
// You must pass in a variable to the get_page function. If you pass
in a value (e.g. get_page ( 123 ); ), WordPress will generate an
error. By default, this will return an object.