开发者

WordPress modifying parent code

开发者 https://www.devze.com 2023-02-18 08:34 出处:网络
I\'ve been told child themes are the way to go, but that you should try your best to not touch the parent templa开发者_运维问答te files and make modifications through action/filter hooks. Often, howev

I've been told child themes are the way to go, but that you should try your best to not touch the parent templa开发者_运维问答te files and make modifications through action/filter hooks. Often, however, I find that I need to insert a <div class="myclass"> or similar into a place where there is no existing hook.

Is there a better way to modify parent code? I find the easiest thing to do is just copy over the file I want to modify, such as header.php, and then making the changes I need. Of course, if the parent theme is updated, my header.php will be out of date, and finding the changes will be a pain!


There are better ways to use child themes that don't involve copying files from the parent theme and hacking them.

Theme frameworks

You will probably have encountered theme frameworks like Thesis, Carrington or Thematic before.

The idea behind a theme framework is that it will give you a flexible foundation for child theme development by:

  • providing a clean, solid, well-structured Parent theme
  • providing additional hooks for your markup, and
  • in some cases, providing admin tools that allow you to modify the theme without coding.

http://codex.wordpress.org/Theme_Frameworks

Function overrides

With a theme framework, it is easy to override existing functions using your functions.php. That allows you to replace common functionality like headers and footers with your own custom code, and also to extend the theme with functions that are not in the chosen theme framework.

Here are some examples for the Thematic framework (I've been using Thematic on a recent project):

  • http://www.catswhocode.com/blog/wordpress-how-to-easily-create-a-thematic-child-theme
  • http://www.showtimedesigner.com/01/2011/wordpress/helpful-thematic-functions-to-customize-your-wordpress-site

So all you should have to modify in your child theme is your style.css and functions.php. This allows your theme to keep functioning even when Wordpress and the underlying Parent theme are updated.


Here's what I did. It's not the cleanest solution, but it works.

The following header.php file essentially loads the parent theme's header.php as a string, inserts code, saves a temporary file, and then includes the temporary file in itself for execution.

$whole_file = file_get_contents(__DIR__ . "/../parent/header.php"); // Load the header file and convert to string
$predecessor_line = '<body id="for-example">'; // We're inserting our code right after this line. As long as the parent theme doesn't update this line, we're good.
$split_file = explode($predecessor_line, $whole_file); // Slice the file at the $predecessor_line (The text used to slice disappears, so be sure to add it again)
$code_to_insert = '<div class="myclass">'; // This is the code you want to insert.
$new_file_content = $split_file[0] . $predecessor_line . $code_to_insert . $split_file[1]; // Piece everything together
$new_file = fopen("_temp_header.php", "w"); // Create a Temporary File
fwrite($new_file, $new_file_content); // Write The Temporary File
fclose($new_file); // Close the Temporary File
include("_temp_header.php"); // Include the Temporary File to execute
unlink("_temp_header.php"); // Delete the Temporary File
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号