WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add php to index.php (11 posts)

  1. Eric Hoanshelt
    Member
    Posted 2 years ago #

    On the index.php file of my theme, there is a section of html that I would like to add some php too. The html looks like this:

    <div id="content">
    	<div class="col-full">   
    
    		<div id="main" class="col-left">

    I would like to add some php to it using the functions.php file (best practice) but I'm not sure how to do it if its possible. The php i would like to add is

    <?php
        			$page = "/";
        			$currentpage = $_SERVER['REQUEST_URI'];
        			if($page==$currentpage) {
        				gravity_form(1, true, false, false, '', false);
    			}
     		?>

    Is there a hook or filter I can use in order to accomplish this?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Not based on the code you gave above. All you could so is create a function and call that function within index.php.

  3. Eric Hoanshelt
    Member
    Posted 2 years ago #

    Thank you for the reply!

    I've had it like this and it works fine but I don't like editing the index.php of the parent theme do to updates. I put the new index.php file in a child theme and made the edits. Is that the best solution then?

    <div id="content">
    	<div class="col-full">
    <?php
        			$page = "/";
        			$currentpage = $_SERVER['REQUEST_URI'];
        			if($page==$currentpage) {
        				gravity_form(1, true, false, false, '', false);
    			}
     		?>
    		<div id="main" class="col-left">
  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Absolutely the best approach! :-)

  5. shirazdrum
    Member
    Posted 2 years ago #

    Well the proper way of doing it would be to define your function in a function.php and put that in your child theme. then you can call it inside your index.php. That's what the function.php is for. This way you are not crowding your documents, and you can use the same function over and over without writing all the codes.

    Example:

    <?php
    function blah_blah(){
    //your code}
    ?>

    and then call the function like:

    <?php blah_blah(); ?>

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    Ok - agreed but that's nit-picking. ;-) If you only want to use a given code snippet once, there is a very valid argument for not making it into a function.

  7. shirazdrum
    Member
    Posted 2 years ago #

    I confess, I'm super anal :) The reason I got in the habit of making functions even for one argument is that when I build sites for clients, I try to eliminate the chances of them removing something that shouldn't have. One single line of code is easier to understand than a long multiple line. And they are more petrified to dig into the function.php than editing the serving files. Believe it or not, even the ones that can't read still mange to edit the files and it's my job to fix things. The fact that it looks prettier doesn't hurt either, but that's at the end of my list :P

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    Hey - I'm something of a function fanatic myself but isn't there something to be said about weighing "pretty code" against the (admittedly minute in this case) performance hit that's incurred when code carries across 2 files? Added to which, ehoanshelt seems pretty clueful.

  9. shirazdrum
    Member
    Posted 2 years ago #

    1 millionth of a second compare to 10 minutes of my trouble shooting time is negligible. I look at the function.php as my private area that i'm the only editor. It comes useful if he tries to change the theme, then all he/she? needs to do is to take one look at the function list and it saves time again.

    With all the times that it saves over 10 years, I can afford a bathroom break once in a while :)

  10. Eric Hoanshelt
    Member
    Posted 2 years ago #

    Email box blew up lol I would use the functions.php file to do it like shirazdrum suggested but this is a one time use only thing and decided to do it inline for reasons esmi expressed. Additionally, the client for this website will never get the appearance tab to touch the files to mess with them ;)

    But thank you both for your time and consideration!

  11. Eric Hoanshelt
    Member
    Posted 2 years ago #

    Marking topic resolved

Topic Closed

This topic has been closed to new replies.

About this Topic