0

I would like to create a page template in my child theme called "Services".

This template is identical to page.php - except that I just want to add some JSON LD (ie, https://schema.org/Service) to these pages.

However I don't want to create a copy of page.php template (eg servicepage.php) as I'm concerned that in the event of a theme update, that my template will become "orphaned"... that is, page.php will get changed fundamentally on the parent theme, but my servicepage.php file will not reflect those changes.

As such:

Is there a smarter way of creating a template so that I don't need to use a "snapshot" of the entire page.php file, but could (eg) only include the changed (schema) code, and then call the original page.php for the rest of it - so that it always matches the parent theme's page.php template otherwise?

I hope that all makes sense - I am open to another solution but so far a template seems to be the easiest way to insert this code on these services pages.

Thanks!

9
  • You should look at hooks in this case. They could be EXTREMELY helpful. Add an action to the page template and execute it in your functions.php
    – rudtek
    May 7 at 6:46
  • Thanks Rudtek! I don't understand the intended approach based on your comment. Perhaps you would be so motivated as to provide an answer with more details?
    – Drewdavid
    May 7 at 16:47
  • This seems like a valid concern (just copying page.php and thus leaving your new template frozen in time) but I can't find any discussion or documentation about this....
    – Drewdavid
    May 7 at 16:53
  • Could this be as simple as making using an include to pull in page.php and then just adding my JSON LD eg below that??
    – Drewdavid
    May 7 at 17:08
  • that's the same idea yes. It's hard to provide an example of general hooks, but if you can't figure it out I'll write up an overall tutorial for you.
    – rudtek
    May 7 at 18:57
1

There are many ways to figure this out. I think, based on your comments the best solution is to use purely hooks. I'm still not exactly sure what you're adding, here are some ideas. (your suggestion of including the page.php in a separate page just won't work because all code is being pulled from the page.php already.

1. add custom code to the footer of a set page. (hooked wp_footer)

In your child theme's functions.php add this code:

function dd_custom_function() {
    if (is_page('services')) { //add your slug or id  or other conditional here that you want to call your code
        echo '<p>This is inserted at the bottom</p>';
    }
}
add_action( 'wp_footer', 'dd_custom_function');

(if you want to add to the header, change wp_footer to wp_header)

2. Put your code in a separate file and pull it into your footer.

add the code file custom.js to your child theme directory in /js/ (create that directory)

add this code to your functions.php of your child theme: (choose one of the wp_enqueue lines..one is for the header one is for the footer.)

function dd_enqueue_function() {
    if (is_page('services')) { //add your slug or id  or other conditional here that you want to call your code
        wp_enqueue_script('creative-blog-main-script', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), null, true); // Here in this example the last value has been set as true, so, it will be loaded in the footer.
        wp_enqueue_script('creative-blog-script', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), null, false); // Here in this example the last value has been set as false, so, it will be loaded in the header.
    }
}
add_action('wp_enqueue_scripts', 'dd_enqueue_function');

3. See if your theme has any actions set you can a hook into.

Many themes will set actions for you that you can hook into. It may even be in the footer.php look for do_action and use those actions to place your custom code. If it doesn't, you could add one line to the page.php file:

do_action('dd_cool_action');

wherever you want the code to execute.

Then in your child 'custom function call the code to action:

add_action('dd_cool_action', dd_custom_function', 5);

function dd_custom_function { ?>
    <div> Drew David custom code activated </div>
<?php }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.