wordpress how to prevent load specific js in specific page - php

I want to prevent specific js in specific page for example suppose i have page name is detail_event and i would like to prevent load script mouse.min in this page .
I wrote following code in curent theme's functions.php but still mouse.min js is loading how to stop it.
add_action( 'wp_enqueue_scripts', 'my_register_javascript', 100 );
function my_register_javascript() {
if ( is_page('my-page') ) {
wp_deregister_script( 'mouse.min' );
}
}
And location of mouse.min js is mysite.com/wp-includes/js/jquery/ui/mouse.min.js?ver=1.11.4
please anyone know how to do this.

There are couple of things to be noticed here:
The handle for mouse.min.js is jquery-ui-mouse and not mouse.min
You are deregistering the script which means that as soon as that page is visited, WP will stop enqueuing this script in all pages.
Instead of doing that you should try below mentioned code:
add_action( 'wp_footer', 'custom_wp_dequeue_script', 11 );
function custom_wp_dequeue_script() {
if ( is_page('my-page') ) {
wp_dequeue_script( 'jquery-ui-mouse' );
}
}

You just need to make the conditional include negative:
if ( !is_page('my-page') ) {

Related

Remove Wordpress'/Woocommerce's wc-blocks-style

This is the css I'm trying to remove (image from Google Page Speed Insights). I put this in my functions.php file but I'm still getting the same result when I rerun Google Page Speed Insights
add_action( 'wp_enqueue_scripts', 'remove_block_css', 9999999999999999 );
function remove_block_css() {
wp_dequeue_style( 'wc-blocks-style' ); // WooCommerce
}

wordpress redirect to login form

I know that are some answers to my question but i don't know if my problem exists in the other persons who asked for help.
I have this function that helps me to redirect users to login before access any contetnt of my website:
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url(get_permalink()));
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
That function works fine but i have a problem. When somebody is navigated to my home page and click to a link that goes here: 'example.com/about' then my function works great. When somebody he trying to go direct to 'example.com/about' by entering this into browser url then my function doesn't work and he is able to access my website. What i have do wrong? I have write my function in my templates function.php file.
I just created a page template called restriction_page.php:
<?php
/*
Template Name: My Restricted Page
*/
?>
<h1>Hello There</h1>
<?php
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url());
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
?>
i set this template as the fefault template in my about page. No solution.
Any ideas?
You can use the built in auth_redirect() function for this:
function restrict_access_if_logged_out(){
if ( !is_user_logged_in() ){
auth_redirect();
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
Note: This should go in your themes functions.php file
Method :1
you can add it to the top of page.php
If you want to add more pages, just add the ||(or) operator like so:
<?php
$pageTitle == get_the_title();
if($pageTitle == 'Submit' || $pageTitle =='NewPage') {
if (!is_user_logged_in()) {
redirect code
} else {
page code
}
} else {
page code
}
?>
Method:2
Write this into a plugin:
add_action( 'template_redirect', 'auth_redirect' );
This will force all visitors login if they aren’t already.
In some cases, this is asking for a log-in every time. This might work better:
add_action( 'template_redirect', function() {
is_user_logged_in() || auth_redirect();
});
You should create a template page for restricted page like we create full width page tamplate, right sidebar page template, left sidebar template, etc. And in restricted page template call your function. When going to create pages, choose restricted template from the listed of the page template in right side.
If it doesn't help you, we'll be able to help you, when you paste your more code with more detail.

How To Make A Function That Links To a Certain File In WordPress

Ok, so im working on a theme that I want to have different styles and php files depending on which design the user has chosen. How would I do this? The best example I can give is having a get_template_part function that changes directory based on the users selection of design. Heres my idea of how the code would kinda look.
<?php /*custom_function*/(/*Global Path variable which changes*/, '/filename') ?>
The x theme has like stacks I think which might be a similar idea. Thanks.
Maybe something like this in the function.php: (just an example)
<?php
define("DESIGN", "flat");
if(DESIGN == "flat"){
add_action( 'wp_enqueue_scripts', 'asd_scripts' );
}elseif(DESIGN == "notflat"){
// other add_action( 'wp_enqueue_scripts', '...' );
}
function asd_scripts() {
// load bootstrap js
wp_enqueue_script('asd-bootstrapjs', get_template_directory_uri().'/includes/resources/bootstrap/js/bootstrap.js', array('jquery') );
}
?>
You need to use the theme customization api to achieve this. For example you would use the customize_register hook:
function mytheme_customize_register( $wp_customize )
{
//All your sections, settings, and controls will be added here
}
add_action( 'customize_register', 'mytheme_customize_register' );
This hook allows you access to the $wp_customize object.
There are existing examples in the WordPress docs, this should get you started with what you would like to do.

body onLoad on a specific WordPress page

Using the latest Wordpress version (3.6), I am trying to apply:
<body onLoad="window.scroll(0, 150)">
To a specific landing page on my website so that the page scrolls to a specific point on page load.
Given the way that Wordpress is set up - with the body tags included in header.php - how can I echo the above code within the body tag for my specific page only, without applying it to the rest of my pages?
I am presuming something needs to go in here:
<body <?php body_class(); ?><?php my code in here?>>
You don't need to apply the code to the <body> tag, see: https://stackoverflow.com/a/191318/1287812
This can be done in functions.php with the following code, and using the conditional tags mentioned by SrikanthAD:
add_action( 'wp_footer', 'b5f_on_load_script' );
function b5f_on_load_script()
{
// Not our page, do nothing
if( !is_page( 'about' ) )
return;
?>
<script type="text/javascript">
window.onload = function() { alert( 'Inside specific page' ); };
</script>
<?php
};
Also see: How to use window.scroll to automatically scroll on pageload?
For some reason, I was having difficulties using the above approach, however the logic was correct.
I have now instead registered the scroll script separately and enqueued it based on page template using:
wp_register_script( 'scroll', get_template_directory_uri() . '/js/scroll.js' );
if( is_page_template('my-landing-page.php') ) wp_enqueue_script( 'scroll' );
With: window.onload=function(){window.scroll(0, 140)}; as a separate file.
Thanks for all the help.
You can use many of the conditional tags available in WordPress to get the desired functionality.
http://codex.wordpress.org/Conditional_Tags
Example:
if ( is_page( 'about' ) ) {
// do something
}
I agree with brasofilo, you don't necessarily have it include the conditional tag in your template. It can used anywhere, depending on your project.

The difference between wrapping a require_once inside a conditional statement and not doing so in functions.php

Without the conditional statement:
require_once( dirname( __FILE__ ) . "/edit_info_check_password.php" );
works fine.
But by simple adding:
if( substr($_SERVER["REQUEST_URI"], 18, 13) == "edit-userinfo" )
require_once( dirname( __FILE__ ) . "/edit_info_check_password.php" );
inside the functions.php file, then the functions inside the required file do not work completely. But they work partially. Why can't I keep the condition if i only want to include the file for a certain page?
EDIT - additional requested info:
basically trying to add the following actions inside the required file:
add_action( 'wp_enqueue_scripts', 'admin_ajax_setup' );
add_action( 'wp_ajax_ajax-checkpasswords', 'check_info_passwords' );
add_action("wp_ajax_nopriv_ajax-checkpasswords", "check_info_passwords");
The admin ajax setup function runs perfectly, it enqueues two javascript files and localized the admin ajax for javasscript access. That's great, but my jquery ajax function returns response 0. Whereas without the conditional statement, I get the full response. Obviously adding the condition changes the way functions.php works with required files.
The reason this does not work is because your ajax admin hooks are not being called.
The reason the ajax hooks are not called is because they are not called at the "edit-userinfo" page. Those ajax calls are handled at a different page. Your require will only load on the "edit-userinfo" page.
The best solution I have is to wrap the ajax hooks in the init hook and the add an is_admin conditional check.
This means that you will need to include your file without the conditional. I don't think performance should be an issue if that file is included every time.
You setup your file like this:
if( substr($_SERVER["REQUEST_URI"], 18, 13) == "edit-userinfo" )
add_action( 'wp_enqueue_scripts', 'admin_ajax_setup' );
function init_admin_ajax_hooks()
{
if (is_admin()) {
add_action( 'wp_ajax_ajax-checkpasswords', 'check_info_passwords' );
add_action("wp_ajax_nopriv_ajax-checkpasswords", "check_info_passwords");
}
}
add_action('init', 'init_admin_ajax_hooks');
I left your enqueue script hook as is because I'm not sure if it's in the admin or not.
If it is in the admin you should use admin_enqueue_script hook instead:
if( substr($_SERVER["REQUEST_URI"], 18, 13) == "edit-userinfo" )
add_action( 'admin_enqueue_scripts', 'admin_ajax_setup' );

Categories