Where do i add custom PHP code in Wordpress custom template? - php

Hy all! From what I read, the best way to add PHP code to a wordpress page would be via a custom template. So i took the page.php from the theme and customised it with html code without any problems. The problem is with the PHP code. No matter where I add it, it doesn't work.
My question is where do I add the custom PHP code for the form validation?
The page looks like this:
/*
Template Name: example
*/
<?php
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>
<section id="container" class="<?php echo tempera_get_layout_class(); ?>">
<div id="content" role="main">
<?php cryout_before_content_hook(); ?>
<?php get_template_part( 'content/content', 'page'); ?>
I added the HTML code here:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> " >
.....
</form>
<?php
endif;
get_footer();
?>

According to this, you will add it before the HTML, near the top of your code.

Well basically it's PHP and should work, if you want to validate the form in the same template file, use an empty action attribute:
<form method="post" action="">
.....
</form>
And make sure not to use an WordPress reserved word on your form element name attribute, for example name.
At the top of the file you can verify and validate all the request being sent by the form as any other PHP file.
<?php
if (!empty($_POST)) {
//validate or do something here
}
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>
Hope this solution help you out.

I'm thinking you should be modifying the content-page.php template. Note the page template calls it with:
<?php get_template_part( 'content/content', 'page'); ?>
Within the content template you should see something like
<?php the_content(); ?>
If you add the form html on the next line, it will be within the same div, something like
<div class="entry-content>
</div>
This way the new content will fall within the same container as other content and be styled as such.

Related

displaying wordpress pages not showing the correct format

I am using this PHP Code on my website to get wordpress page content from the database:
$page = get_page_by_id($records["value"], OBJECT, 'page');
query_posts('p='.$page->ID.'');
echo '<div id="page-title">'.$page->post_title.'</div>';
echo nl2br($page->post_content);
echo '<p> </p><hr /><p> </p>';
in wordpress, i have put the page with HTML and aligned images etc but when it displays on my website it just shows the images and text all under each other with the incorrect format (not as i put it in the text editor)
what have i done wrong here?
Im also wondering why you are doing like this? there is no need to call post title and content that way. simply make a page.php. The TwentyFourteen does have page template but in a folder named "page-templates". You can simply make a page.php by sample code below.
<?php get_header(); ?> //the header template
<?php while ( have_posts() ) : the_post(); ?> //start the loop
<?php the_title(); ?> //Page Title
<?php the_content(); ?> //The content
<?php endwhile; ?> //end loop
<?php get_sidebar(); ?> //sidebar template
<?php get_footer(); ?> // footer template
you can add div / span wherever you want.

Hide Div title if there's no content

I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.

how to add standard elements in php in one line

I have created a few php files Following are their names:
standard_head.php (which contains basic standard html code)
header.php
navbar.php
sidebar.php
footer.php
standard_footer.php (which contains closing html tags)
Page content will vary in every page and would also include html content.
I am trying to create a lot of php pages which will use all of the above pages directly.
Now I can use them directly using include statement for each of them, but I was wondering if it was possible to include all of them together with one just statement ?
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
You could so this (note that this is a very basic example and that I wouldn't use this myself without taking into account meta tags, page titles etc.)
template.php
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $content; ?>
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
Then, on a page (say index.php, for example):
index.php
<?php
$content = '<h1>Welcome</h1>';
include 'template.php';
?>
P.S: If you go down this route, make sure that you check out the Output Control Functions first.
Just create a PHP file that has a list of them i.e.
<?php include("this file");
include("the other file");
?>
And then just add that file.
Not really an answer to your question, but you could use a template engine like Twig.
http://twig.sensiolabs.org/
Try using more complex template system like Smarty or some MVC framework like Zend (this is not required but would allow you to create complex sources more easily) and then build script like this:
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $menu->getCustomEntries(); ?>
</div>
Where $menu will be your custom object containing methods for displaying menus and submenus...
There is no straight way to include multiple files, as include / require functions except only one argument. though you can use following logic.
`#include_all_files.php
include('../includes/standard_head.php');
include('includes/header.php');
...
use above file in other files
include('includes/include_all_files.php');
`

Wordpress, wrap posts in div

I'm quite new to wordpress, so bear with me. I have a site up and running allowing me to add content dynamically, the only problem is styling each post. I'm looking to wrap posts in a div, so images would be wrapped in one div and text posts would be wrapped in another, if that makes sense?Here's some code I've been using:
<?php if ( in_category('photos') ) { ?>
<div id="testing">
<?php } else { ?>
<div id="test-2">
<?php } ?>
The only problem with this is that it wraps all posts in a given category and doesn't put a closing '< / div >' after the content, if that makes sense?
Well, the only reason you're not getting any closing </div>s is because you're not putting it anywhere in your php.
This should work:
<?php while( have_posts() ) : the_post()
if( in_category( 'photos' ) { ?>
<div id="testing">
// All your post content and whatnot
<?php the_content(); ?>
</div>
<?php } else { ?>
<div id="test-2">
// All your post content
<?php the_content(); ?>
</div>
<?php }
endwhile; ?>
If you don't write complete markup in your theme files, you can't possibly expect Wordpress, or PHP, to automatically close your HTML tags.
"if that makes sense" No it doesn't really make sense as Wordpress by default adds a CSS-class for every category and tag to the surrounding div. If that is not the case in your theme, use the function post_class() to add the CSS-classes to your div.
See the post_class()-documentation for usage examples.

Wordpress Text is not Shows line by line

I write a custom type for wordpress which is contain a textarea. I can save it and also i can access from single view. But i want to show them line by line if author save it line by line. But it's not showing them line by line, it shows all of them as a single line.
Here is my single.php's custom meta show codes;
<div id="yemek_resim">
<?php if($tz_image_display == 'true') : ?>
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : /* if post has post thumbnail */ ?>
<?php the_post_thumbnail('single-large'); ?>
<?php endif; ?>
<?php endif; ?>
</div><!-- yemek_resim -->
<div id="yemek_malzemeleri">
<?php
$malzemeler = get_post_meta($post->ID, 'ofa_yemek_malzemeler', true);
echo esc_attr($malzemeler);
?>
</div><!-- yemek_malzemeleri -->
here yemek_malzemeler's content come from a textarea and author can write multiline content. But at site, they seem as a single line...
You should use the N2BR PHP function in the output file. Just write <?php echo n2br($string); ?> in the single.php file from Wordpress. Good luck ;)
I resolved the problem by using <pre> </pre> tags. thanks for your care.

Categories