• Hello, I am new to WP so it’s a bit of a learning curve but an easy one. Since WP uses hooks and short code which is nice. I am unable to find the some code I am needing to edit.

    Where is the core code for where the widgets are built? In my themes file for sidebar.php it has this which is nothing but hooks. Where would I find those hooks where the core code for the widget is constructed?

    sidebar.php

    <?php global $theme; ?>
    
    <div id="sidebar-primary">
    
        <?php
            if(!dynamic_sidebar('sidebar_primary')) {
                /**
                * The primary sidebar widget area. Manage the widgets from: wp-admin -> Appearance -> Widgets
                */
                $theme->hook('sidebar_primary');
            }
            $theme->hook("sidebar_primary_after");
        ?>
    
    </div><!-- #sidebar-primary -->
    
    <div id="sidebar-secondary">
        <?php
            if(!dynamic_sidebar('sidebar_secondary')) {
                /**
                * The secondary sidebar widget area. Manage the widgets from: wp-admin -> Appearance -> Widgets
                */
                $theme->hook('sidebar_secondary');
            }
        ?>
    
    </div><!-- #sidebar-secondary -->
Viewing 11 replies - 1 through 11 (of 11 total)
  • What theme are you using?

    Thread Starter Texan78

    (@texan78)

    I am using this one.

    http://newwpthemes.com/demo/NewsAgency/

    I wouldn’t think the theme would matter as it is core WP code but I could be wrong.

    I don’t have access to your theme’s code, so I can’t say for sure, but it looks like your theme registers various custom hooks which you can use for various PHP functions. What exactly are you trying to accomplish?

    Thread Starter Texan78

    (@texan78)

    I have some code I am using to hide some Google AdSense code when it is accessed via mobile browser. It works fine as I have used it on other adds. I just need to access the core code for the widget so I can insert the code. I have tried wrapping the code around it in the same text box from the admin console where the code is pasted but the php isn’t rendered. So looks like I need to directly edit the file. I am just not sure where that code is so I can do that.

    What file would normally have the code for widgets?

    I’m sorry to ask so many questions, but I’m still a bit unclear on what you’re doing. Are you using one of the Google AdSense plugins or does your theme contain a custom widget that replicates the functionality?

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    The core code for the widgets is located in the wp-includes folder. The default widgets is: default-widgets.php and the class is widgets.php

    Thread Starter Texan78

    (@texan78)

    Thanks Jose, those was the file I was needing but, unfortunately this code I was needing wasn’t in there. When widgets are installed, what file does the code install into where it is actually displayed in the sidebar since the sidebar.php is just the hooks?

    @stephen, no worries, I am not good and explaining things. What I have is a regular widget that allows you to enter any text or HTML in it. You can’t put php in it though because for some reason it doesn’t render it which it should. So in this text box I have pasted my Google AdSense code in it to display a Google Ad in the sidebar. That works fine but, when you view it from a mobile device the add breaks the template. So I have this code that I wrap around the Google Ads code that disables Google Ads on mobile devices. I just need to find where the code is that displays that widget in the sidebar so I can wrap this code around it.

    <?php if(isMobileBrowser()==0 || isMobileBrowser()==false){ ?>
    <----- GOOGLE AD OR WIDGET OUTPUT CODE GOES HERE -------->
     <?php } ?>
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    The function that calls the ‘widgets’ in general is dynamic_sidebar( ).

    So let’s say you have two widgets in the “primary sidebar” and want to use it only when you are on a certain page. You would use it something like:

    if ( is_page( 'contact' ) ) {
      dynamic_sidebar( 'primary-sidebar' );
    }

    Do keep in mind that the name/id of the sidebar is what is used when you register the sidebar(s).

    I want to say there is a way to get the widget content but I could be wrong; core has changed and you find new, and cool, things when you read through the source code. 🙂

    As you’ve discovered, the default WP text widget won’t execute PHP code. There are a few plugins that provide a text widget that will execute PHP code. Another option would be to edit your functions.php in a child theme and put:

    add_filter('widget_text', 'php_text', 99);
    function php_text($text) {
        if (strpos($text, '<' . '?') !== false) {
            ob_start();
            eval('?' . '>' . $text);
            $text = ob_get_contents();
            ob_end_clean();
        }
        return $text;
    }

    That code will filter the text widget output to execute PHP code.

    Thread Starter Texan78

    (@texan78)

    Thanks Jose, I did find that out looking through the code. Finding out where the “primary-sidebar” code is constructed is a little difficult but, I think I found a work around for this.

    @stephen, that worked great. If I wanted to use it on a different widget I would just change the “widget_text” to the widget I wanted to use it with?

    To be honest, I’m not sure. From a quick glance at the filter reference, it looks like the text widget is the only widget that provides a builtin hook. Depending on what you need, you may have to write a custom widget or try searching the plugin repository.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Widget core code file’ is closed to new replies.