PHP in WordPress
-
So I have a few plugins that allow me to use PHP in my WordPress, but all of them output my PHP output at the very bottom of the page. The output is obviously near the bottom of my code (it has to be), but is there a way to force this to the top?
-
kinda depends on how you set it up. Any details?
depends on the how your theme is build.
you could hook it into your wp_head action to achieve that..
wp_head action is mostly located in your header.php file.
if there is no wp_head action , just addwp_head();function into your header file.example on how to hook into your wp_head action:
function test(){ echo "hello from inside the test action"; } add_action('wp_head', 'test', 10);you could also create your own actions anywhere you desire.
example:
1. add a action into your header or any theme file as:
do_action("your_action_name");2. crate a function in your function.php file and use add_action to hook the function into your action as:
//create a function function myFunc(){ #your code, for example echo "hello from inside my function"; } //add action add_action('your_action_name', 'myFunc', 10); //add_action() function hooks into your action and prints the function code on the page where your do_action() is locatedthis article explains more on how to create and use actions:
http://codex.ww.wp.xz.cn/Function_Reference/do_actionSure, here’s some more info. This is some of my code:
<?php LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE LOT OF CODE echo " <center><table border=0 width=90% cellpadding=0 cellspacing=0> <tr> <th width=60% align=left><x4>Venue</x4></th> <th align=left><x4>Special</x4></th> </tr>"; while($row = mysql_fetch_array($result)) { $venue[$i] = $row['establishment']; $site[$i] = $row['site']; $deal[$i] = $row['body']; $address[$i] = $row['address']; $phone[$i] = $row['phone']; $query2="SELECT * FROM <code>users</code> WHERE <code>establishment</code> = $venue[$i]"; $results=mysql_query($query2); #$num_rows = mysql_num_rows($result); echo "<center><table border=0 width=90% cellpadding=0 cellspacing=0>"; echo "<tr>"; echo "<td valign=top width=60%> <hr> <x5><a href=" . $site[$i] . " class=x5>" . $venue[$i] . "</a>" . "</x5></br><x8>" . $address[$i] . "</x8></br><x7>" . $phone[$i] . "</x7></br>" . "</td>"; echo "<td valign=top><hr></br><x6>" . $deal[$i] . "</br></br></br> </td>"; echo "</tr></table>"; } echo "<hr width=90%>"; } ?>I cut out probably 100 lines of code, and then it echoes the table near the bottom of the code. Here’s what I am getting when I open the page in my browser (sorry for having to mark out info, it’s a person’s personal info).
Top of page: Top of Page
And when I scroll down: Bottom of Page
If I insert this exact same code into a non themed php file, it prints at the top. No clue why inserting it into wordpress is causing it to print at the bottom.
normally if I place hardcoding above the content area/loop in a page or post.php file, that code outputs above the loop.If I put it below, it outputs below the content.
I can’t tell fron this where you are putting it, but did you try it before the loop?
I’m not sure exactly what you’re asking, but the loop is near the bottom of the code, mainly because it relies on variables from the top part in able to work correctly.
if it needs to be in the loop then I’d assume it’s something which applies to each entry.. and not just single table on the page. I’m not really sure what to ask, just feeling for a better understanding of this set up. feels like I’m looking through a keyhole, this can be difficult work, but I’m game.
Maybe if I explained it better it would help. I checked and I only removed 30 lines of code. The top portion of code just connects to a DB, and then selects rows based on a criteria.
Then right before I cut off above it’s just saying while rows are found…then the code I posted starts. So when it finds rows it outputs the table.
Not sure why it causes it to push to the bottom of the page, I tested other themes and it does the same.
So this is entirely custom? not within any standard wp theme template? I’m afraid Imay not be of much help with that.
I can see where the echo output has v-align code, did you try playing with it? are you certain it written correctly?
also have you tried to do it with css?
The topic ‘PHP in WordPress’ is closed to new replies.