• Resolved mbcoulter

    (@mbcoulter)


    Howdy,

    I have a some very basic mods I need to perform in the functions.php file. I now want to move these modifications out into my child theme’s functions.php file.

    This is the function in question which works fine in parent theme’s functions.php file.

    public function get_branding() {
      $site_title = get_settings( 'blogname' );
      $site_url = esc_url( get_site_url() );
      $branding_lower = '<h2 class="branding-lower"><a href="/">xxxxxx</a></h2><h2 class="branding-department"><a href="'.$site_url.'">yyyyyy</a></h2>';
      $branding = '<section class="branding"><h1 class="branding-upper"><a href="/">zzzzzz</a></h1><span class="branding-sep"> | </span>'.$branding_lower.'</section>';
      return $branding;
    	}

    And when I place it in my child theme’s functions.php file I modify the function name and call it like this:

    public function get_branding_child_ThemeName() {
      ...blah, blah, blah...

    And I have created a copy of the theme file that calls this function and modified it to use the child theme name:

    echo ThemeName::get()->get_branding_child_ThemeName();

    The error I receive is not very helpful:

    Parse error: syntax error, unexpected ‘public’ (T_PUBLIC) in C:\Users\me\Documents\Websites\test.dev\wp-content\themes\ThemeName-child\functions.php on line 27

    And line 27 is this line in my childtheme’s functions.php file:
    public function get_branding_child_ThemeName() {

    Any help would be greatly appreciated!

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi!

    Seeing this:

    public function get_branding()

    Leads me to think the theme is using a class. The error you are receiving is super helpful:

    Parse error: syntax error, unexpected ‘public’

    Emphasis added, of course. What this means is you have an extra public in that line. I’m guessing you are not using this code in a class, correct? If you are really wanting to use that code then you would need to create an extending class. You can find some useful information here:
    https://php.net/manual/en/keyword.extends.php

    In that class you would just use something like:

    class JMC_child extends ParentTheme_Class {
      public function get_branding(){
        // your code here
      }
    }

    And then you would use something like:

    echo JMC_child::get_branding();

    Keep in mind those are examples only to illustrate usage not actual working code.

    If you are not too familiar with PHP classes, PHP.net does provide some useful information as well:
    https://php.net/manual/en/oop4.php

    Let us know if you have any further questions!

    Thread Starter mbcoulter

    (@mbcoulter)

    Hi Jose,

    Thank you SO VERY MUCH! Your comments were very close to the perfect fix. I found this boilerplate included at the bottom of my parent theme’s functions.php file:

    //Start child theme code.
    
    function MYPARENTTHEME_CHILD_THEME() {
      class MY_CHILD_THEME extends MYPARENTTHEME {
        //Define overrides and new functionality here.
      }
      new MY_CHILD_THEME();
    }

    I dropped this into my child theme’s functions.php file (with my theme’s specific names) and it seems to have worked!

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Awesome! Glad to see it worked out for you! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Child theme functions.php for dummies’ is closed to new replies.