• I’m trying to get a child theme working. It seems to do OK with the following in the functions.php file:

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );

    }
    ?>

    But then when I add the code from my customizr functions.php file, then it doesn’t work. Below is the child functions.php code I’m trying. Any suggestions?

    Thanks,
    Sam

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );

    }

    add_filter( ‘get_comment_author_link’, ‘rv_remove_comment_author_link’, 10, 3 );
    function rv_remove_comment_author_link( $return, $author, $comment_ID ) {
    return $author;
    }

    add_filter( ‘tc_default_widgets’ , ‘add_featured_page_widget’ );
    function add_featured_page_widget( $defaults ) {
    $defaults[‘fp_widgets’] = array(
    ‘name’ => __( ‘Featured Pages Widget’ , ‘customizr’ ),
    ‘description’ => __( ‘Above the featured pages area on home’ , ‘customizr’ )
    );
    return $defaults;
    }

    add_action(‘__before_fp’ , ‘display_my_fp_widget’);
    function display_my_fp_widget() {
    dynamic_sidebar(‘fp_widgets’);
    }

    // As of 3.1.10, Customizr doesn’t output an html5 form.
    add_theme_support( ‘html5’, array( ‘search-form’ ) );
    add_filter(‘wp_nav_menu_items’, ‘add_search_form_to_menu’, 10, 2);
    function add_search_form_to_menu($items, $args) {
    // If this isn’t the main navbar menu, do nothing
    if( !($args->theme_location == ‘main’) ) // with Customizr Pro 1.2+ and Cusomizr 3.4+ you can chose to display the saerch box to the secondary menu, just replacing ‘main’ with ‘secondary’
    return $items;
    // On main menu: put styling around search and append it to the menu items
    return $items . ‘<li class=”my-nav-menu-search”>’ . get_search_form(false) . ”;
    }

    //make it open affiliate link immediately if amazon
    remove_action (‘woocommerce_before_shop_loop_item’, ‘woocommerce_template_loop_product_link_open’, 10);
    add_action (‘woocommerce_before_shop_loop_item’, ‘my_link_open’, 10);
    function my_link_open() {
    global $product;
    if( $product->is_type( ‘external’ ) ){
    $producturl = $product->get_product_url();
    $position1 = stripos($producturl, ‘amazon’);
    $position2 = stripos($producturl, ‘redirectAmzASIN’);
    if ($position1 === false and $position2 === false) {
    // no # in sku so not an affiliate product
    $url = get_the_permalink();
    } else {
    // must be an affiliate product
    $url = $product->get_product_url();
    }
    }
    else {
    $url = get_the_permalink();
    }
    echo ‘‘;
    }

    //add javascript to certain pages
    function wpb_adding_scripts() {
    wp_register_script(‘HeightComparisonTool’, get_template_directory_uri() . ‘/js/HeightComparisonTool.js’,”,’1.1′, true);
    wp_register_script(‘GetHeightArray’, get_template_directory_uri() . ‘/js/GetHeightArray.js’,”,’1.1′, true);
    if(is_single( ‘height-comparison-tool-celebrity-height-difference’ )) {
    wp_enqueue_script(‘HeightComparisonTool’);
    wp_enqueue_script(‘GetHeightArray’);
    }
    if(is_page( ‘javascript-testing-2-2’ )) {
    wp_enqueue_script(‘HeightComparisonTool’);
    wp_enqueue_script(‘GetHeightArray’);
    }
    }
    add_action( ‘wp_enqueue_scripts’, ‘wpb_adding_scripts’ );

    //run javascript when a certain page loads, the javascript being run checks the url for queries and populates corresponding html input and run the update
    add_action( ‘wp_footer’, ‘b5f_on_load_script’ );
    function b5f_on_load_script()
    {
    //Not our page, do nothing
    if( !is_single(‘height-comparison-tool-celebrity-height-difference’) and !is_page(‘javascript-testing-2-2’))
    return;
    ?>

    <script type=”text/javascript”>
    window.onload = function() {
    var hashParams = window.location.hash.substr(1).split(‘?’); // substr(1) to remove the #
    if(window.location.href.indexOf(‘#’) != -1){ // No additional info, do nothing
    for(var i = 0; i < hashParams.length; i++){
    var p = hashParams[i].split(‘=’);
    document.getElementById(p[0]).value = decodeURIComponent(p[1]);
    }
    }
    Load(“Page”);
    }
    </script>

    <?php
    };

Viewing 6 replies - 1 through 6 (of 6 total)
  • 1. Have you tried a Child Theme Plugin? Maybe give it a shot
    2. I haven’t had the need to go this far to get a child theme working.
    Try this link:
    https://www.wpbeginner.com/wp-themes/how-to-create-a-wordpress-child-theme-video/

    Hi TallSam,
    Can you elaborate more on the error your get? What doesn’t work?

    Thread Starter TallSam

    (@tallsam)

    I’m afraid to push the activate button, so I’ve just been pressing live preview and it never loads. After more testing, I’ve simplified the problem. The first bit of code below works for live preview, but the second has a bit added and it doesn’t work. Any suggestions?

    Thanks,
    Sam

    First

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
    }
    ?>

    Second

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
    }

    add_filter( ‘get_comment_author_link’, ‘rv_remove_comment_author_link’, 10, 3 );
    function rv_remove_comment_author_link( $return, $author, $comment_ID ) {
    return $author;
    }
    ?>

    why exactly are you afraid to activate it?

    i have done it 3+ times without any issues

    You don’t need to copy code from the theme functions.php file into your child’s theme. WordPress will excute both the parent and child themes functions.php file.

    This code is an exact copy of the parents themes functions.php

    <?php
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    }

    The functions.php file in your child theme overrides the one in your parent theme. If you activate the theme and something goes wrong just rename the functions.php file to something else and the child theme will run the functions.php file from the parent theme.

    Thread Starter TallSam

    (@tallsam)

    I get it now, the issue was that I was duplicating code as it was on both my child and my parent theme functions.php files. Changed that and it works great, no more site breaking when I update, very nice!

    The one thing that I still need to do every time I update though is to redo the changes I make in the image attachment page “next” and “previous” links. I make this to the file: class-content-attachment.php

    Perhaps there is a way to make these changes in functions.php? I’m basically swapping in some hyperlinks that makes it go to the next image in an Media Library Assistant image category rather than just some random old image.

    Any thoughts?

    Thanks,
    Sam

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Trying to get child theme working’ is closed to new replies.