WordPress Plugin - Is It Possible to Programmatically Edit Theme Code

by tpw
6 replies
There is one function that fires in the post comments page (comments.php), which is buried in the theme folder, that I need to edit.

I need to change the code to put the existing function into an IF ELSE statement.

For example:

Code:
if (...)
{
// do original function
}
else
{
// do new function
}
I could tell people, "go to the comments.php file and replace X with Y", but I hate doing that, because people are scared to death of playing with code.

I would rather program a solution that automatically makes this change.

But I cannot seem to locate any tutorial that addresses this.

Am I seeking to do the impossible? Or is there a way to do this that I have not yet thought of?

Thanks in advance.
#code #edit #plugin #programmatically #theme #wordpress
  • Profile picture of the author tpw
    Another option that would work for me is an add_action for the public display of comments... But I cannot seem to find the information for doing that.
    Signature
    Bill Platt, Oklahoma USA, PlattPublishing.com
    Publish Coloring Books for Profit (WSOTD 7-30-2015)
    {{ DiscussionBoard.errors[3933005].message }}
  • Profile picture of the author mikeminneman
    tpw,
    A pretty simplistic answer for how this could be accomplished would be to create a new php file with the specific code for programmatically replacing the code in comments.php, and then telling your users to upload that php file and then run it from their web browser.

    In php, the steps you should follow would be:
    1. Open file for reading
    2. Find the specific text you're looking to replace
    3. Replace it in the string
    4. Write the file

    Code would be like:

    $comments_string = file_get_contents("wp-content/templates/mytemplate/comments.php");
    $code_to_find = "echo $comment";
    $code_to_replace = "if(something){ do whatever; } else { do something else; }";
    $comments_string = str_replace($code_to_find,$code_to_replace,$commen ts_string);
    file_put_contents("wp-content/templates/mytemplate/comments.php", $comments_string);


    Pretty simplistic, I hope this makes sense.
    {{ DiscussionBoard.errors[3933776].message }}
  • Profile picture of the author tpw
    Thanks. It does make sense.

    I was thinking along those lines myself, but eliminated it, when I realized that the theme makers all seem to handle the wp_comments_list code differently.

    I do believe I have found a way to accomplish the desired changes, without ever touching the comments.php in the theme folder.
    Signature
    Bill Platt, Oklahoma USA, PlattPublishing.com
    Publish Coloring Books for Profit (WSOTD 7-30-2015)
    {{ DiscussionBoard.errors[3934062].message }}
  • Profile picture of the author Tim Franklin
    Interesting question, I have a plugin that implements something along those lines and it would require copy pasting code to "activate" the rest of the plugin, it would be interesting to find a method of updating the code, for those "non-technical users" but then the main issue here is this, when wordpress comes out with a new version, which is something that happens all the time, what happens if your changes are over written, that would be a real pain to have to find that code and make your changes all over again, seems like the best solution would be one where you isolate the "custom" code so that it can run independently of any changes made by WP, just a thought,
    Signature
    Bitcoin | Crypto | Blockchain Secrets |
    {{ DiscussionBoard.errors[3935186].message }}
  • Profile picture of the author SteveJohnson
    Is there not a filter in there that you can take advantage of? Where exactly do you need to change things around?
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3935480].message }}
    • Profile picture of the author MoonWolf
      Im looking for something similar but i can't just inject code since i need to change code already there. I was thinking of renaming the comments.php file and place a own file there programaticly that reads in comments.php and makes the changes i would like to be done. Ofcorse that way i can't cover all themes that don't follow the ordinary hieraki of wordpress but atleast 99%.
      {{ DiscussionBoard.errors[4106706].message }}

Trending Topics