WordPress.org

Ready to get started?Download WordPress

Forums

Child functions.php (9 posts)

  1. tvguy2000
    Member
    Posted 1 year ago #

    Hi All, I'm not new to WordPress but I'm new to child functions.php.

    Here is the code as it appears in my theme and I would like to place it in a child functions.php so I can tweak bits of it.

    I already have a child theme set up, just not sure about what to do next. I am just unclear about renaming the function, and I need to do a remove and add as well, right? not sure how to do that though.

    /**
    * Set custom post excerpt link if excerpt is supplied manually.
    */
    function manual_excerpt_read_more_link($output) {
    
    	global $themeslug, $options, $post, $root;
    
    	$linktext = $options->get($themeslug.'_excerpt_link_text');
    	$linktext = $linktext == '' ? 'Continue Reading...' : $linktext;
    
    	if(!empty($post->post_excerpt))
    		return $output . '…</p><div class="more-link"><span class="continue-arrow"><img class="continue_img" src="'.$root.'/images/continue.png"></span><a>ID) . '">  '.$linktext.'</a></div>';
    	else
    		return $output;
    }
    add_filter('the_excerpt', 'manual_excerpt_read_more_link');

    [Please post code snippets between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

  2. catacaustic
    Member
    Posted 1 year ago #

    When you have a child theme, the functions.php file in that theme is loaded automatically before the functions.php file in the parent theme.

    What this means is that you can define the same function name in your child themes functions.php file and that will be used first. BUT, there is one big thing to look out for. The function in your parent theme must be wrapped with a check to see if the function already exists. Good themes do this but it's still a bit patchy sometimes, so you'd need to add it in yourself. As an example, you would have this set up in the parent theme:

    if (!function_exists('manual_excerpt_read_more_link')) {
        function manual_excerpt_read_more_link() {
            // Function code in here.
        }
    }
  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    as the example is a filter function, you can remove the filter first and add your own; in a child theme's functions.php, you will need to add an action to 'after_setup_theme' to do this.

    example, to be added to functions.php in the child theme:

    add_action('after_setup_theme','childtheme_adjust_excerpt_filter');
    
    function childtheme_adjust_excerpt_filter() {
    
    	remove_filter('the_excerpt', 'manual_excerpt_read_more_link');
    	add_filter('the_excerpt', 'childtheme_manual_excerpt_read_more_link');
    
    }
    
    function childtheme_manual_excerpt_read_more_link($output) {
    //edit the section below//
    	global $themeslug, $options, $post, $root;
    
    	$linktext = $options->get($themeslug.'_excerpt_link_text');
    	$linktext = $linktext == '' ? 'Continue Reading...' : $linktext;
    
    	if(!empty($post->post_excerpt))
    		return $output . '…</p><div class="more-link"><span class="continue-arrow"><img class="continue_img" src="'.$root.'/images/continue.png"></span><a>ID)' . '">  '.$linktext.'</a></div>';
    	else
    		return $output;
    }
  4. tvguy2000
    Member
    Posted 1 year ago #

    Thanks Catacaustic, what you said makes total sense.

    An issue for me is that the parent functions.php file on the theme that I'm using does not have the functions I want to tweak wrapped in the check.

    I really like the theme so I'm going to have to approach this another way...by using the filter function, and action. At least, that's what I am thinking.

    John T

  5. tvguy2000
    Member
    Posted 1 year ago #

    Thanks Alchymyth

    Thanks for getting back to me so quickly. I have tried code similar to that which you have proposed, and I even used your code, and when I save the child functions.php file, I get the following error...any ideas?

    Warning: Cannot modify header information - headers already sent by (output started at /home/postpr7/public_html/aquariumcarebasics.com/wp-content/themes/Eclipse-Pro-Child-Theme-2013-02-05/functions.php:31) in /home/postpr7/public_html/aquariumcarebasics.com/wp-includes/pluggable.php on line 876

    Here is the function from the pluggable.php on line 876

    `
    if ( !function_exists('wp_redirect') ) :
    /**
    * Redirects to another page.
    *
    * @since 1.5.1
    * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
    *
    * @param string $location The path to redirect to
    * @param int $status Status code to use
    * @return bool False if $location is not set
    */
    function wp_redirect($location, $status = 302) {
    global $is_IIS;

    $location = apply_filters('wp_redirect', $location, $status);
    $status = apply_filters('wp_redirect_status', $status, $location);

    if ( !$location ) // allows the wp_redirect filter to cancel a redirect
    return false;

    $location = wp_sanitize_redirect($location);

    if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
    status_header($status); // This causes problems on IIS and some FastCGI setups

    header("Location: $location", true, $status);
    }
    endif;

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F

    for example, make sure that there are no characters like spaces, or empty lines, before the first <?php in functions.php

  7. tvguy2000
    Member
    Posted 1 year ago #

    Thanks Alchymyth, the tip about eliminating the extra spaces worked, and the function works fine.

    Following your pattern, I tried my hand (several times!) at trying to tweak another function to no avail. I get the bit about renaming the child function so it does not conflict with the parent function, but I cannot seem to wrap my head around the "add_action", "remove_filter" and "add_filter" bits.

    Here is the code I am trying to get working in my child functions. I want to be able to set the width of the post thumbnail to something longer than 720px. Changing the width is easy...getting the function to work is a little beyond me.

    function ec_theme_setup() {
    	global $content_width;
    	if ( ! isset( $content_width ) ) $content_width = 608; //Set content width
    
    	add_theme_support(
    		'post-formats',
    		array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat')
    	);
    
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 720, 240, true );
    	add_theme_support('automatic-feed-links');
    	add_editor_style();
    }
    add_action( 'after_setup_theme', 'ec_theme_setup' );

    Thanks in advance for all of your help. I am dying to see what you do with this code so I can compare it to my code that didn't work.

  8. tvguy2000
    Member
    Posted 1 year ago #

    Am I on the right track with this?

    add_action('after_setup_theme','childtheme_adjust_ec_theme_setup');
    
    function childtheme_adjust_ec_theme_setup() {
    
             remove_filter('the_excerpt', 'ec_theme_setup');
             add_filter('the_excerpt', 'childtheme_ec_theme_setup');
    
    }
    
    function childtheme_ec_theme_setup() {
    	global $content_width;
    	if ( ! isset( $content_width ) ) $content_width = 608; //Set content width
    
    	add_theme_support(
    		'post-formats',
    		array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat')
    	);
    
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 720, 240, true );
    	add_theme_support('automatic-feed-links');
    	add_editor_style();
    }
  9. tvguy2000
    Member
    Posted 1 year ago #

    well, that didn't work, because I had 'the_excerpt' as the first argument for remove_filter() and add_filter(). So I removed the first argument for both. My code now reads:

    add_action('after_setup_theme','childtheme_adjust_ec_theme_setup');
    
    function childtheme_adjust_ec_theme_setup() {
    
             remove_filter('ec_theme_setup');
             add_filter('childtheme_ec_theme_setup');
    
    }
    
    function childtheme_ec_theme_setup() {
    	global $content_width;
    	if ( ! isset( $content_width ) ) $content_width = 608; //Set content width
    
    	add_theme_support(
    		'post-formats',
    		array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat')
    	);
    
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 720, 240, true );
    	add_theme_support('automatic-feed-links');
    	add_editor_style();
    }

    And predictably, I am getting the following error message:

    Warning: Missing argument 2 for remove_filter(), called in /home/postpr7/public_html/aquariumcarebasics.com/wp-content/themes/Eclipse-Pro-Child-Theme-2013-02-05/functions.php on line 30 and defined in /home/postpr7/public_html/aquariumcarebasics.com/wp-includes/plugin.php on line 260

    Warning: Missing argument 2 for add_filter(), called in /home/postpr7/public_html/aquariumcarebasics.com/wp-content/themes/Eclipse-Pro-Child-Theme-2013-02-05/functions.php on line 31 and defined in /home/postpr7/public_html/aquariumcarebasics.com/wp-includes/plugin.php on line 65

    I'm stumped as to what the remove and add arguments should be, and also whether my approach is on the right track at all.

    Any help regarding this would be sincerely appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.