mp3por
mp3por

Reputation: 1852

Wordpress child theme very small change

I need to change one string in the template of one theme's component. Lets say this is in the header.php. In my child theme I create a header.php file to override the parent's theme header.php.

My question is what do I write in my header.php ? Do I copy paste everything from the parent's version and just change the string ? If 'yes' then how do I ensure that I get updates, because now and forever I would be using this version of the header.php since it will always override the parent's version. If 'no' than what do I do ? One of the files is just a template but I have to make a small string change in the hook.php file as well ... How do I handle that ?

Example: This is an example file from my parent's theme. All I want to change is "Read More" to "Прочети Още".

<?php
if ( ! class_exists( 'WPBakeryShortCode' ) ) {
    return;
}

class WPBakeryShortCode_UpsideGridService extends WPBakeryShortCode {

    protected function content($atts, $content = null) {
        ob_start();
        $atts = vc_map_get_attributes( $this->getShortcode(), $atts );
        extract($atts);

        $up_tags = array();
        if ( !empty($tag_slugs) ) {
            $up_tags = explode(',', $tag_slugs);
        }

        $query = array(
            'post_type'      => array('service'),
            'posts_per_page' => (int)$total_item,
            'post_status'    => array('publish')
        );

        if ( $up_tags ) {
            $query['tax_query'] = array(
                array(
                    'taxonomy' => 'service-tag',
                    'field'    => 'slug',
                    'terms'    => $up_tags
                ),
            );
        }

        $results = new WP_Query( $query );
        if ( $results->have_posts() ) :
            switch ( $element_layout ) {

                case 'three_items_small_size':
                    $widget_before = '<div class="kopa-ab"><div class="wrapper"><div class="row"><div class="col-md-12"><div class="widget kopa-intro-3-widget">';
                    $widget_after = '</div></div></div></div></div>';
                    $widget_head = $this->the_head($element_layout, $title, $content);
                    $widget_before_loop = '<div class="widget-content"><ul class="row ul-mh">';
                    $widget_after_loop = '</ul></div>';
                    break;

                case 'four_items':
                    $widget_before = '<div class="widget kopa-service-1-widget kopa-area-dark">';
                    $widget_after = '</div>';
                    $widget_head = $this->the_head($element_layout, $title, $content);
                    $widget_before_loop = '<div class="widget-content"><ul class="ul-mh row">';
                    $widget_after_loop = '</ul></div>';
                    break;

                case 'three_items_big_size':
                    $widget_before = '<div class="widget kopa-service-2-widget">';
                    $widget_after = '</div>';
                    $widget_head = $this->the_head($element_layout, $title, $content);
                    $widget_before_loop = '<div class="widget-content"><ul class="ul-mh row">';
                    $widget_after_loop = '</ul></div>';
                    break;

                case 'five_items':
                    $widget_before = '<div class="widget kopa-service-3-widget">';
                    $widget_after = '</div>';
                    $widget_head = $this->the_head($element_layout, $title, $content);
                    $widget_before_loop = '<div class="widget-content"><div class="kopa-masonry-2"><ul class="kopa-masonry-wrap">';
                    $widget_after_loop = '</ul></div></div>';
                    break;
            }

            echo wp_kses_post( $widget_before );
                echo wp_kses_post( $widget_head );
                    echo wp_kses_post( $widget_before_loop );
                        while ( $results->have_posts() ) {
                            $results->the_post();
                            $this->the_loop($element_layout, $element_width, $atts);
                        }
                        wp_reset_postdata();
                    echo wp_kses_post( $widget_after_loop );
            echo wp_kses_post( $widget_after );
        endif;

        $output = ob_get_clean();
        return $output;
    }

    public function the_loop($element_layout='four_items', $element_width='3', $atts = array()) {
        extract($atts);

        if ( '5' == $element_width ) {
            $item_class = 'ms-item-2 col-custom-5';
        } else {
            $item_class = sprintf('col-md-%s col-sm-%s col-xs-12', esc_attr($element_width), esc_attr($element_width));
        }
        $service_linkto = get_post_meta(get_the_ID(), 'upside-kidhouse-linkto', true);
        if ( empty($service_linkto) ) {
            $service_linkto = get_the_permalink();
        }
        $service_icon = get_post_meta(get_the_ID(), 'upside-kidhouse-icon', true);
        if ( false === strpos($service_icon, 'fa ') ) {
            $service_icon = 'fa ' . $service_icon;
        }
        $service_title = get_the_title();
        if ( ! isset($excerpt_length) || empty($excerpt_length) ) {
            $excerpt_length = 55;
        }
        $GLOBALS['upside_excerpt_length'] = (int) $excerpt_length;
        add_filter('excerpt_length', 'upside_set_excerpt_length');
        switch ( $element_layout ) {
            case 'three_items_small_size': ?>
                <li class="<?php echo esc_attr($item_class); ?>">
                    <article class="entry-item">
                        <?php if ( ! empty($service_icon) ) : ?>
                            <span class="kopa-icon-1 <?php echo esc_attr($service_icon); ?>"></span>
                        <?php endif; ?>
                        <div class="entry-content">
                            <?php if ( ! empty($service_title) ) : ?>
                                <h4 class="entry-title"><a href="<?php echo esc_url($service_linkto); ?>" title="<?php echo wp_kses_post($service_title); ?>"><?php echo wp_kses_post($service_title); ?></a></h4>
                            <?php endif; ?>
                            <?php the_excerpt(); ?>
                        </div>
                        <a href="<?php echo esc_url($service_linkto); ?>" class="more-link-1"><i class="fa fa-angle-right"></i></a>
                    </article>
                </li>
                <?php break;
            case 'four_items': ?>
                    <li class="<?php echo esc_attr($item_class); ?>">
                        <div class="sv-item-1">
                            <?php if ( ! empty($service_title) ) : ?>
                                <h4><a href="<?php echo esc_url($service_linkto); ?>"><?php echo wp_kses_post($service_title);?></a></h4>
                            <?php endif;
                            if ( ! empty($service_icon) ) :
                            ?>
                                <div class="sv-icon-1">
                                    <a href="<?php echo esc_url($service_linkto); ?>">
                                        <span class="<?php echo esc_attr($service_icon); ?>"></span>
                                    </a>
                                </div>
                            <?php endif; ?>
                            <?php the_excerpt(); ?>
                        </div>
                    </li>
                <?php
                break;
            case 'three_items_big_size':
            ?>
                <li class="<?php echo esc_attr($item_class); ?>">
                    <div class="kopa-item-wrap">
                        <div class="kopa-item sv-item-2">
                            <div class="sv-item-content">
                                <?php if ( ! empty($service_icon) ) : ?>
                                    <span class="<?php echo esc_attr($service_icon); ?>"></span>
                                <?php endif; ?>
                                <?php if ( ! empty($service_title) ) : ?>
                                    <h5><a href="<?php echo esc_url($service_linkto); ?>" title="<?php echo wp_kses_post($service_title); ?>"><?php echo wp_kses_post($service_title); ?></a></h5>
                                <?php endif;
                                    the_excerpt();
                                ?>

                                <a class="kopa-btn-3" href="<?php echo esc_url($service_linkto); ?>"><?php esc_html_e('+ Read More', 'upside'); ?></a>
                            </div>
                        </div>
                    </div>
                </li>

            <?php
                break;

            case 'five_items':
                ?>
                    <li class="<?php echo esc_attr($item_class); ?>">
                        <article class="entry-item">
                            <?php if ( ! empty($service_icon) ) : ?>
                                <div class="entry-icon-1">
                                    <span class="<?php echo esc_attr($service_icon); ?>"></span>
                                </div>
                            <?php endif;?>
                            <h4 class="entry-title"><a href="<?php echo esc_url($service_linkto); ?>"><?php echo wp_kses_post($service_title); ?></a></h4>
                        </article>
                    </li>
                <?php
                break;
        }

        remove_filter( 'excerpt_length', 'upside_set_excerpt_length' );
    }

    public function the_head($element_layout='four_items', $title, $content) {
        $output = '';
        switch ( $element_layout ) {
            case 'three_items_small_size':
            case 'four_items':
            case 'three_items_big_size':
                if ( ! empty($title) || ! empty($content) ) {
                    $output .= '<header class="widget-hd-1">';
                        if ( ! empty($title) ) {
                            $output .= sprintf('<h3 class="widget-title s1">%s</h3>', esc_html($title));
                        }
                        if ( ! empty($content) ) {
                            $output .= sprintf('<p>%s</p>', wp_kses_post($content));
                        }
                    $output .= '</header>';
                }
                break;
            case 'five_items':
                if ( ! empty($title) || ! empty($content) ) {
                    $output .= '<header class="widget-hd-1">';
                    if ( ! empty($title) ) {
                        $output .= sprintf('<h3 class="widget-title s4">%s</h3>', esc_html($title));
                    }
                    if ( ! empty($content) ) {
                        $output .= sprintf('<p>%s</p>', wp_kses_post($content));
                    }
                    $output .= '</header>';
                }
                break;
        }
        return $output;
    }
}

add_action( 'vc_before_init', 'upside_map_grid_service' );
function upside_map_grid_service() {
    $grid_cols_list = array(
        array( 'label' => esc_attr__("6 items", 'upside'), 'value' => 2 ),
        array( 'label' => esc_attr__("5 items", 'upside'), 'value' => 5 ),
        array( 'label' => esc_attr__("4 items", 'upside'), 'value' => 3 ),
        array( 'label' => esc_attr__("3 items", 'upside'), 'value' => 4 ),
        array( 'label' => esc_attr__("2 items", 'upside'), 'value' => 6 )
    );

    vc_map( array(
        "name" => esc_attr__("Service Grid",'upside'),
        "base" => "UpsideGridService",
        'category' => esc_attr__( 'Upside', 'upside' ),
        'description' => esc_attr__('Display some services.', 'upside'),
        "icon" => apply_filters('upside_k_get_shortcode_icon', 'upside-icon', 'UpsideGridService'),
        "params" => array(
            array(
                "type" => "textfield",
                "admin_label" => true,
                "group" => esc_attr__("General", 'upside'),
                "heading" => esc_attr__("Title", 'upside'),
                "param_name" => "title",
                "value" =>"",
                "description" => '',
            ),
            array(
                "type" => "textarea",
                "group" => esc_attr__("General", 'upside'),
                "heading" => esc_attr__("Description", 'upside'),
                "param_name" => "content",
                "value" => "",
                "description" => '',
            ),
            array(
                'type' => 'exploded_textarea',
                "group" => esc_attr__("General", 'upside'),
                'heading' => esc_attr__( 'Tag slugs', 'upside' ),
                'param_name' => 'tag_slugs',
                'description' => esc_attr__( 'Enter tag slugs (Note: divide tag slugs with linebreaks (Enter)).', 'upside' ),
            ),
            array(
                'type' => 'dropdown',
                "group" => esc_attr__("General", 'upside'),
                'heading' => esc_attr__( 'Display type', 'upside' ),
                'param_name' => 'element_layout',
                'value' => array(
                    array( 'label' => "Default four items per row", 'value' => 'four_items' ),
                    array( 'label' => "Default three items per row with big size", 'value' => 'three_items_big_size' ),
                    array( 'label' => "Default three items per row with small size", 'value' => 'three_items_small_size' ),
                    array( 'label' => "Default five items per row", 'value' => 'five_items' ),
                ),
                'std' => 'four_items',
                'edit_field_class' => 'vc_col-sm-12 vc_column',
                'description' => esc_attr__( 'Select layout to display.', 'upside' ),
            ),
            array(
                "type" => "textfield",
                "group" => esc_attr__("General", 'upside'),
                "heading" => esc_attr__("Total items", 'upside'),
                "param_name" => "total_item",
                "value" =>"12",
                "description" => '',
            ),
            array(
                'type' => 'dropdown',
                "group" => esc_attr__("General", 'upside'),
                'heading' => esc_attr__( 'Number items per row', 'upside' ),
                'param_name' => 'element_width',
                'value' => $grid_cols_list,
                'std' => '3',
                'edit_field_class' => 'vc_col-sm-12 vc_column',
                'description' => esc_attr__( 'Select number of services per row.', 'upside' ),
            ),
            array(
                "type" => "textfield",
                "group" => esc_attr__("General", 'upside'),
                "heading" => esc_attr__("Number words of excerpt to show", 'upside'),
                "param_name" => "excerpt_length",
                "value" =>"55",
                "description" => '',
                'dependency' => array(
                    'element' => 'element_layout',
                    'value' => array( 'four_items', 'three_items_big_size', 'three_items_small_size')
                )
            ),
        )
    ) );

}

Upvotes: 1

Views: 216

Answers (2)

Christian Langer
Christian Langer

Reputation: 16

  1. Create child folder in your wp themes folder in the same directory like your parent theme, e.g. myTheme-child
  2. Copy the complete header.php in this directory but with this line of change
<a class="kopa-btn-3" href="<?php echo esc_url($service_linkto); ?>"><?php esc_html_e('+ Прочети Още', 'upside'); ?></a>
  1. Wp overrides all .php´s from parent theme with the .php files you have in myTheme-child

    Update:

Alternative solution using Wp Hook with add_filter function:

  1. Create functions.php in myTheme-child
  2. Copy following code into it:
 <?php 
 /* Child theme functions file
 */

 function custom_readMore( $more ) 
 { return '<a class="kopa-btn-3" href="<?php echo > > esc_url($service_linkto); ?>"><?php esc_html_e('+ Прочети Още', 'upside'); ?></a>'; }

 add_filter( 'excerpt_more', 'custom_readMore' );
  ?>

The functions.php is an exception, Wp always look into functions.php of parent theme first and then executes the functions in myTheme-child.

The header.php in myTheme-child will replace the file from parent theme so you will loose the updates from your parent theme.

Hope this helps!

Upvotes: 0

Anass
Anass

Reputation: 59

If you are working with a child theme, you will want to copy all contents from the original header.php file in the parent theme to a file in your child theme folder of the same name. After you have the header.php file in your child theme folder, you can then open and edit the contents.

When you use a child theme, you must copy the updated header.php file from the parent theme, if you do not want to loose customization, you should then make a comparison between the header that you're using on the child theme, and the new updated header of the parent.

Some available software offers comparison options between two files to detect modifications that you have made.

Upvotes: 1

Related Questions