Jquery Method...

Ok, as of now I am adding my jquery snippets like this...

1. I add something like this in my theme functions.php:

# REMOVE ADMIN FOOTER TOP WIDGET FOR NON-ADMINS
function hide_footer_top() {
  if( !current_user_can( 'super_admin')) {
	wp_enqueue_script( 'my-hide-footer-top', 'http://dreamfirehub.com/wp-content/themes/parallelus-mingle/my-hide-footer-top.js', array('jquery'), '1.0', true);
} }
add_action('admin_print_scripts-widgets.php', 'hide_footer_top');

2. Then in a separate jquery file called my-hide-footer-top, I add:

/********** REMOVE ADMIN FOOTER TOP WIDGET FOR NON-ADMINS **********/
jQuery(document).ready(function(){
jQuery("div#widgets-right div.widgets-holder-wrap:nth-child(4)").css("display","none");
});

But, what I would really like to be able to do (for sake of speed and simplicity) is be able to just specify my jquery snippets directly in functions.php. I would like to be able to add something like:

# REMOVE ADMIN FOOTER TOP WIDGET FOR NON-ADMINS
function hide_footer_top() {
  if( !current_user_can( 'super_admin')) {
  echo '<style type="text/javascript">
	jQuery(document).ready(function(){
        jQuery("div#widgets-right div.widgets-holder-wrap:nth-child(4)").css("display","none");
});
} }
add_action('admin_print_scripts-widgets.php', 'hide_footer_top');

... directly to functions.php and call it good. Is this possible? If so, what is the correct format? Seems like everything I try using the proposed method does not work for some reason. What gives?

Thanks!

  • Han
    • The Crimson Coder

    Hi coreymj78,
    Yes you can put all those code in functions.php. Can you try to replace the last line
    add_action('admin_print_scripts-widgets.php', 'hide_footer_top');
    with
    add_action('wp_print_footer_scripts', 'hide_footer_top');

    if your theme uses wp_footer at footer, you can try this as well
    add_action('wp_footer', 'hide_footer_top');

  • aecnu
    • WP Unicorn

    Greetings coreymj78,

    We have not heard form you in sometime therefore I am assuming this issue is resolved?

    If so Happy Days! No nee to reply.

    If not please feel free to mark this ticket as not resolved below or we may miss it.

    In addition, please include and new errors or symptoms that are happening if any.

    Cheers, Joe :slight_smile:

  • rozani
    • Site Builder, Child of Zeus

    Hi coreymj78,

    Actually, what are you try to achieve?

    add_action('admin_print_scripts-widgets.php', 'hide_footer_top'); is not good method.

    It should be like this:
    add_action('admin_print_scripts-widgets', 'hide_footer_top');

    Still I don't understand why is must be add_action instead of add_filter.

    Rozani

  • coreymj78
    • Site Builder, Child of Zeus

    Ok, sorry for the delay, RIYAKU thanks for that code. I need to be able to specify the unique page to run the script on as i dont want to run the script on all pages. So what is the correct format in which to do this in FUNCTIONS.PHP (with both functions and script in same file) instead of only enqueing/registering the script from functions.php only?

    Thanks.

    P.S. If you don't put the .php in the add_action then the code doesn't work, at least not when your only enqueing/registering a separate script file from functions.php for a certain page.

  • Han
    • The Crimson Coder

    @coreymj78, can you try this,

    function hide_footer_top() {
      if( !current_user_can( 'super_admin') && is_front_page()) {
      echo '<style type="text/javascript">
    	jQuery(document).ready(function(){
            jQuery("div#widgets-right div.widgets-holder-wrap:nth-child(4)").css("display","none");
    });
    } }
    add_action('wp_print_footer_scripts', 'hide_footer_top');

    let us know :slight_smile:

  • coreymj78
    • Site Builder, Child of Zeus

    Ok, that is not what I want, that targets the home page only. What if I wanted to target a different page like a buddypress page or an /about page? Alternatively, how can I target an admin side page using this method?

    Thanks!

  • coreymj78
    • Site Builder, Child of Zeus

    Ok I'm at a loss how to do this. How can I target a buddypress page (profile page for example) with a jquery script and put it all in functions.php? To put these types of code blocks in functions.php it seems to be an issue of where you put your php tags (<?php). Is it that the php tags go on either side of the actual <script> tags?

    I'm confused

  • coreymj78
    • Site Builder, Child of Zeus

    Ok can I do it with if ( (bp_is_my_profile() .... or some variation of that?

    Also, how could I use the above conditional to register / enqueue a script for the buddypress profile page?

    Something like this perhaps (although the below code does not work):

    function buddypress_profile_layout() {
    global $theLayout;
    if ( bp_is_my_profile() ) : {
    wp_register_script( 'buddypress-profile-layout', 'http://dreamfirehub.com/wp-content/themes/parallelus-mingle/my-jquery/buddypress-profile-layout.js', array('jquery'), '1.0', true);
    wp_enqueue_script( 'buddypress-profile-layout' );
     } endif;
    }
    add_filter('wp_enqueue_scripts', 'buddypress_profile_layout');

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.