• Resolved ehoanshelt

    (@ehoanshelt)


    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?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Not based on the code you gave above. All you could so is create a function and call that function within index.php.

    Thread Starter ehoanshelt

    (@ehoanshelt)

    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">

    Absolutely the best approach! ??

    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(); ?>

    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.

    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 ??

    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.

    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 ??

    Thread Starter ehoanshelt

    (@ehoanshelt)

    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!

    Thread Starter ehoanshelt

    (@ehoanshelt)

    Marking topic resolved

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Add php to index.php’ is closed to new replies.