How to add php to a wpmu page???
-
I have a php mySQL database search, etc and I want it to work in a wordpress page I will create called “Search”. How can I put PHP into a wordpress page?
I already asked this over at mu.wordpress.org no answer yet.
-
There’s lots of different ways of doing this. You can create a plugin and then put your code in there. Call it from any wp page. You can also create a page template and put your code directly in the ‘search’ template. That’s probably what you are looking for.
Start here: https://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
The general idea is that all pages are created with a specific template. You create your own, probably by copying an existing page template and modifying it, and then create a page called ‘Search’ that uses that template. You build whatever functionality you want into the template.
I followed the Codex and created the template, it looks correct, but when I click “submit” to execute the script…I just get the index page. Any ideas on that? Here is my page code.
<?php
/*
Template Name: Browse ExBriefs
*/
?>
<?php get_header(); ?>
<div id=”content” class=”narrowcolumn”>
<h2>Search</h2>
<form name=”search” method=”post” action=”<?=$PHP_SELF?>”>
Seach for: <input type=”text” name=”find” /> in
<Select NAME=”field”>
<Option VALUE=”first”>First Name</option>
<Option VALUE=”last”>Last Name</option>
<Option VALUE=”city”>City</option>
<Option VALUE=”state”>State</option>
<Option VALUE=”country”>Country</option>
<Option VALUE=”gender”>Gender</option>
</Select>
<input type=”hidden” name=”searching” value=”yes” />
<input type=”submit” name=”search” value=”Search” />
</form>
<?PHP
//This is only displayed if they have submitted the form
if ($_POST[searching] ==”yes”)
{
echo “<h2>Results</h2><p>”;
//If they did not enter a search term we give them an error
if ($_POST[find] == “”)
{
echo “<p>You forgot to enter a search term”;
exit;
}
// Otherwise we connect to our Database
include(“http://www.ex-brief.com/profiles/dbinfo.inc.php”);
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
// We preform a bit of filtering
$find = strtoupper($_POST[find]);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data=mysql_query(“select * from contacts where “.$_POST[field].” like ‘%”.addslashes($find).”%'”);
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result;
echo ” “;
echo $result;
echo “
“;echo $result;
echo “
“;echo $result;
echo “
“;echo $result;
echo “
“;echo $result;
echo “
“;echo “
“;}
//This counts the number or results – and if there wasn’t any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo “Sorry, there is no Ex-Brief for <b>”$find”</b> yet
Please try again.
“;
}
}
?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I’m not sure what you mean by:
“but when I click “submit” to execute the script…I just get the index page.”
What submit? You aren’t going to be executing a script at all. wp does that when a user clicks on the link for the page.
I think the error might be in your use of php_self
<form name=”search” method=”post” action=”<?=$PHP_SELF?>”>
What does the html look like when you view the page? I have a feeling it might be putting the wrong url into the form?
Also…
<input type=”text” value=”bob” name=”s” id=”s” />
Change that name and id, because “s”, is caught by wordpress and shows you a search result page:
That is a wordpress page not found result, basically.
That is why you search isn’t working.
You may find this helpful:
https://codex.wordpress.org/Creating_a_Search_Page
Hope that helps,
Brad
i figured this out, I had to split the code up into seperate html and php files and make 2 templates. it works fine now. thanks
- The topic ‘How to add php to a wpmu page???’ is closed to new replies.