Files included in wordpress header.php are not accessible of page templates even after calling get_header() - wordpress

I am developing a custom wordpress template. I have a few page templates for the layout.I call get_header() and get_footer() at the top and bottom of my page templates respectively.
Now the issues is. I have used two or three require_once() in the header.php file to include php class file. And in one of the included file , I have created an object for the included class files. But when I call those objects in my pages files (-- where I used get_header()--) it says undefined variable.
This is my wordpress header.php
// THIS IS MY header.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(session_id() == '')
session_start();
date_default_timezone_set('Asia/Dubai');
require_once('risk-profiler/configuration.php'); // Config file
require_once('risk-profiler/dal.php'); // One class files
require_once('risk-profiler/bl.php'); // another class file
require_once('risk-profiler/form_handler.php'); // Were Objects for the classes are created
?>
form_handler.php
if (!isset($data))
$data = new DataAccessLayer(sql_server, sql_user, sql_password, sql_database);
if (!isset($bl))
$bl = new businessLogic;
$data is my object for the database class and $bl is an object of another class.
Now this is where I call get_header() risk_profile_questionnaire.php and I included two form (risk-profile-select-profiling-country.php & another.php) in this file(form) is where I call that object and it is not accessible.
risk_profile_questionnaire.php is as
<div class="form-group">
<label for="" class="col-md-4 control-label">Country : </label>
<div class="col-sm-8">
<select class="form-control input-lg" name="version_details">
<?php
$version_details = $data->get_version_list();
while ($row = mysqli_fetch_array($version_details)) {
echo"<option value='" . $row['country_code'] . "|" . $row['version'] . "'>" . $row['country_name'] . "</option>";
}
?>
</select>
</div>
</div>
Can anyone helpme on why my objects are not accessible at that point.

I can't test now, but my guess is that this is because of variables scope.
If you are going to use a global variable inside a function in PHP, you need to declare it as global at the beginning of the function.
Since you are including header.php (and the rest of files included from header.php) from the function "get_header", variables are limited by default to the scope of "get_header" function.
Try to declare the global variables you need to use at the beginning of header.php file, for example:
global $data;
Variables scopes in PHP: http://php.net/manual/en/language.variables.scope.php

Related

How to design a php page theme template in wordpress using either html or css?

<?php
/*
Template Name: isbn
*/
?>
<form action="" method="post" name="myForm">
Filter <input id="isbn" type="text" name="isbn" />
<input type="submit" name="submit" value="Submit" /></form>
<?php get_header(); ?>
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$table_name = "isbn"; // change this to your table name
$field = $_POST['isbn']; // change this to your isbn field $_POST['ISBN'];
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name where isbn = '".$field."'");
foreach ($retrieve_data as $retrieved_data) {
echo $retrieved_data->title;
// echo $retrieved_data->image; // for image
}
}
?>
This is a search form which i want to design. I have created this form in a page template named isbn. But when i am opening that page for editing i am unable to do this. I am using divi theme in wordpress.
So divi theme is not allowing me to edit this page. Due to which this page is looking very bad in look wise.
Can anyone help me for designing this page by giving their codes or simply by giving their suggestions?
I am facing one more problem whenever i am writing css code in above code i am not getting anything. So i am totally blank that how to deal with this
Just create a WordPress page and add shortcode which you added in functions.php before. Don't create template for this page just use your shortcode only.
First, you have to create a page using divi theme, then create shortcode for this form in functions.php and then use this shortcode in your page.
you are inserting the form above the get_header(); function, so it will be outside of the html tag and outside of the body tag. Move it into the body, then it should at least appear on the page, and you'll see what you need to do next.

Converting spaghetti code to Twig

Need to convert script with "spaghetti code" to Twig. Read Twig documentation and got basics working. However, I need advice on how to do everything properly, so no re-conversion is needed later. Let's say current script looks following:
file index.php:
<?php
$page_message="do it";
function display_dropdown($max)
{
for ($i=0; $i<$max; $i++)
{
echo "<option value='$i'>Option $i</option>";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<h1><?php echo $page_message; ?></h1>
<form method="post" action="<?php echo basename($_SERVER["SCRIPT_FILENAME"]); ?>">
<select name="whatever"><?php display_dropdown(10); ?></select>
<input type="submit" value="go">
<?php include("footer.php");?>
</body>
</html>
footer.php looks:
<?php
$footer_text="blah blah";
?>
<footer><?php echo $footer_text; ?></footer>
As far I understand, my index.php should look like this when converted to Twig:
<?php
$page_message="do it";
function display_dropdown($max)
{
for ($i=0; $i<$max; $i++)
{
echo "<option value='$i'>Option $i</option>";
}
}
$twig_params_array=array("page_message"=>$page_message, "footer_text"=>"blah blah");
require_once("../lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader=new Twig_Loader_Filesystem("templates");
$twig=new Twig_Environment($loader);
echo $twig->render("index_template.html", $twig_params_array);
?>
Then I should create index_template.html and footer_template.html (or whatever) with following code:
index_template.html
<!DOCTYPE html>
<html>
<body>
<h1>{{ page_message }}</h1>
<form method="post" action="{{ _self }}>">
<select name="whatever"><?php display_dropdown(10); ?></select>
<input type="submit" value="go">
{{ include('footer_template.html') }}
</body>
</html>
footer_template.html
<footer>{{ footer_text }}</footer>
If I understand right, it's also possible to "include" functions in Twig templates (with some tweaking in templates), so I don't need to rewrite existing PHP functions like display_dropdown(). Because dropdown is not displayed at the moment...
The thing that concerns me is array with variables (passed to Twig render function). Do I miss something, or is it really needed to manually define each variable (like $page_message and $footer_text) before Twig can work?
It seems like a lot of work to do, because in "spaghetti code" if I define variable somewhere, I can access it at any time just by using echo function. Now, it looks I need to view every single variable that exists in PHP code and manually pass it to Twig parameters array. Really?
Since no solution was provided, I found it myself. All you have to do is use such a line of code, so all the variables you have in your PHP file (counting included files) become available in Twig templates, and you don't need to re-write anything when new variables are added to PHP file later (they also automatically become available in Twig):
echo $twig->render("template_file_name.extension", get_defined_vars());
Mindaugas, just changing the technology you use to render views is not going to make the code less tangled.
I think you should go one step further and divide responsibilities, by using an MVC framework, like Symfony, which uses Twig as its default templating language.
Good luck!

Custom metabox for file upload return empty filename

I want to attach a file to a post and "do something with it later". The file isn't being pulled over when I publish/update my post, the error I get is an empty filename. If I change the input type to text and submit I can get the text to save/display but trying to upload the file acts as though I haven't supplied the file to the form:
form --
function display_file_upload_meta_box($post_id,$post){
wp_nonce_field( basename( __FILE__ ), 'file_upload_meta_box_nonce' );
?>
<p>
<?php
$fileUpload= get_post_meta($object->ID,'file-upload-meta',true);
if(!$fileUpload)
$fileUpload = '';
echo 'file: '.$fileUpload;
?>
<label for="file_upload_meta">Attach a file to this post</label>
<input type="file" id="file_upload_meta" name="file_upload_meta" class="widefat"/>
</p>
<?php
}
code to upload file --
$new_meta_value = wp_upload_bits($_FILES["file_upload_meta"]['name'], null, file_get_contents($_FILES["file_upload_meta"]['tmp_name']));
It's probably because the form does not have those attributes :
enctype="multipart/form-data" encoding="multipart/form-data"
You can use a hook to add them, check this : https://gist.github.com/rfmeier/3513349
I got a requirement for adding custom meta boxes for a custom post type in wordpress and I tried using the following plugin and got working for me. Try this

How to add a class to a Drupal 7 region?

I am trying to add a .clearfix class to my footer region in a Drupal 7. Is there a way to do this?
I am currently using the following to print my footer region:
<?php print render($page['footer']); ?>
Which outputs:
<div class="region region-footer">
<div id="block-1>....</div>
<div id="block-2>....</div>
</div>
Here's the code snippet:
function MY_THEME_NAME_preprocess_region(&$variables, $hook) {
if($variables['region'] == "MY_REGION_NAME"){
$variables['classes_array'][] = 'MY_CLASS_NAME';
}
}
Or if you'd rather insert the class into all of the regions:
function MY_THEME_NAME_preprocess_region(&$variables, $hook) {
$variables['classes_array'][] = 'MY_CLASS_NAME';
}
Copy region.tpl.php (found in modules/system directory) to your theme directory. Then copy everything inside it and create a new file. Paste into that file and make any changes you like to the template. Once finished, save it as region--footer.tpl.php and clear the cache on your site to see the changes.
The region.tpl.php contains (along with a lot of comments explaining possible variables):
<?php if ($content): ?>
<div class="<?php print $classes; ?>">
<?php print $content; ?>
</div>
<?php endif; ?>
So all you would need to do is add a class on that DIV.
It is even better if you use a hook, you can use template_preprocess_region.
Try adding the include to the footer.php.tpl file. You may have to create it.

Home Page Only Footer

I have a blog, http://sweatingthebigstuff.com and I would like to add an extra line in the footer which will display only from the homepage. I found this code, but it is not working for me. Do I have the wrong syntax or is there something else I can try to get this to work?
<?php if ( is_home() ) { ?>
text
<?php } ?>
Here is where footer.php is called
<?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
<div class="cleared"></div>
<?php get_footer(); ?>
And here is the footer code:
text 2
Contact | Disclaimer | Privacy StatementCopyright © 2009-2010 Sweating The Big Stuff. All Rights Reserved.
and then some sitemeter crap.
is_home() sets a global var that doesn't seem to reset itself or re-evaluate, wp kinda strange.
Try putting wp_reset_query() at the end start of your if statement code
Actually, it'll be better to call it before as we can ensure the queries are reset
<?php wp_reset_query();
if ( is_home() ) { ?>
text
<?php } ?>
Now that the php is working, ideally you'd want the code above.
text
I just did a view source and I can plainly see the php code, which shouldn't be visible since it is meant to be parsed server side. The following shouldn't be there in the view source.... wrong file being edited?
<p>
<?php if ( is_home() ) { ?>
text
<?php } ?>
<wp_reset_query()>
<br />
<br />
The footer.php file should be located in wp-content/themes/nameofyourtheme folder
is_home() is a method that should return true or false. You need to implement this method somewhere. If blogspot doesn't implement this method for you, you need to do it yourself. For your website I think this function would do what you want:
<?php
function is_home(){
$r = $_SERVER['REQUEST_URI'];
return $r == '/' || $r == '' || $r == '/index.php';
}
if(is_home()) {
?>
text
<?php } ?>
And where you want the footer, put:
<?php include 'footer.php'; ?>
instead of the line:
<?php get_footer(); ?>
I believe your problem is that get_footer() is reading the footer as text, so it isn't executing the PHP beforehand. If you do it this way you can add as much PHP in the header as you want.
There is nothing wrong with your syntax, and when I view your page source, I see " text "
What is the file extension of your footer page? if it is "footer.php" then I shouldn't see the opening and closing terms for php (). php won't run unless the file extension is ".php"
as to a previous answer:
<?php wp_reset_query();?> should go BEFORE <?php if(is_home()){?>text<?php } ?> in this scenario. is_home() depends on a loop being present on the page. Maybe somewhere you used a custom query, or one of your plugins used a custom query that upset the default query vars. Like I said, use the reset statement before your if statement.
You need to modify the code to include a check for is_front_page() like so:
<?php
$ishomepage = ( is_home() || is_front_page() );
switch( $ishomepage )
{
case true :
echo 'Your homepage-only snippet of text goes here';
break;
case false :
default :
// Do nothing... or do something else...
break;
}
?>
Reference: WordPress Codex: is_front_page()
Default WP installations don't have a homepage defined, it uses your index.php and checks for other templates like a home.php template as a starting point (home.php only if your theme has it.) Here's a diagram from their online docs that shows how their hierarchy works: WordPress Template Hierarchy.
By default, WordPress shows your most recent posts on the front page
of your site. But many WordPress users want to have a static front
page or splash page as the front page instead. This "static front
page" look is common for those who wish to not have a "blog" look to
their site, giving it a more CMS (content management system) feel.
If you want to know how to set a static homepage, read this article and follow the instructions: Creating a Static Frontpage. When you set a static front page, is_home() will work as expected.

Resources