How to save PHP code written in Wordpress page - php

I'm using Artisteer to create WordPress themes.
Artisteer is not able to include php code into pages, then I have to do some modification via WordPress page editor.
I'm using "PHP Insert" plugin to include php code into WordPress page editor and to make WordPress able to use my php code into my pages.
In my site I have a form for B.M.I. calculation and I need to use php code in the page.
The problem is that if I change the theme in Artisteer and if I upload it into WordPress, I loose php changes.
It's very boring to apply modification every time (and also dangerous...lost code, wrong code position,...).
I'm thinking to use a child theme, but I don't know how to apply it to php code added into pages.
Suggestions?
Thanks in advance

The Wordpress team has made it a rule of thumb to never execute PHP in posts and pages either because it can be hectic to change things in the future and may expose security vulnerabilities that could not be fixed by Wordpress itself.
A better practice is editing your WordPress theme by creating a custom post/page template - that basically can include everything. However for some situations, it may be necessary to execute PHP on more than one WordPress posts/pages.
In this case, my recommendation would be to create a simple page template containing your PHP, or, if your code is used multiple times, create a shortcode (in the functions.php of your template).
Simplest example of adding a shortcode tag (with parameters) to your themes functions.php:
function footag_func( $atts ) {
return "foo = {$atts['foo']}";
}
add_shortcode('footag', 'footag_func');
The output code can be used everywhere with the tag [footag foo="bar"]. When extending this, alway remember to return the results of your function. Using echo or print would just echo the results (on the top of your content, skipping the content itself).

You're not supposed to use PHP in the content editor, you've got a few decent options:
Creating a custom template for that specific page:
http://codex.wordpress.org/Page_Templates
Creating a shortcode for that specific piece of PHP code:
http://codex.wordpress.org/Shortcode_API

Related

How to display custom text below site logo image with function / filter in wordpress

I needed to display my site slogan (about four words) below the site logo image, my theme author told me to add the text after certain codes in header.php. This works but my worry is that the change will be overridden after theme update because i don't use child them, i prefer using code snippet plugin to add code to my site.
I need a function / filter that i can add to my function.php to enable me achieve this without losing the change after theme update.
Can anybody help with this?
Install the PHP Code Snippet addon for WordPress, then you can put php code wherever you want on your site inline:
https://wordpress.org/plugins/insert-php-code-snippet/
From the developers site:
Insert PHP Code Snippet allows you to create shortcodes corresponding to PHP code snippets. You can create a shortcode corresponding to any random PHP code such as ad codes, login validation etc. and use the same in your posts, pages or widgets.
The shortcodes generated using the plugin are easily available as a dropdown in the standard wordpress content editor as well as in widget settings, thereby giving you ease of integrating your PHP snippets with your posts and pages.
Users Guide:
https://help.xyzscripts.com/docs/insert-php-code-snippet/user-guide/
Download link:
https://downloads.wordpress.org/plugin/insert-php-code-snippet.1.3.2.zip

PHP code in wordpress page

I have a working wordpress theme. I don't have any plugins installed in my theme. When I created a new page and wrote PHP code to echo a single line, it does not show anything. I edit the wp_config file and set define('WP_DEBUG', false) to define('WP_DEBUG', true)......but nothing happen, the code is below
<?php
echo 'gggggggggggggg';
?>
When inserting your PHP code into wordpress pages, there are two recommended ways:
make a child theme
write your own plugin
Making a child theme is faily easy and a common way for little PHP extensions.
There is also a plugin available to insert PHP code into pages here.
You should avoid to put PHP files into the internal Wordpress directories or modify wordpress files since these modifications get lost with updates which happen to be quite often in Wordpress
You cannot write PHP in pages directly and its not recommended to do so, because you are opening doors to world of security threats.
The best way to write is, by creating templates(wirte your PHP there) and then assign it to some page.
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/#creating-custom-page-templates-for-global-use

Wordpress - creating custom pages with php code

I need to add few additional pages to my wordpress site.
These pages should not be "part of the site", ie they should not be linked somewhere from the posts and so on.
However, they should have the same header/footer as the rest of the site ( I am using custom theme ). And they should be accessible via url.
The final requirement is, I should be able to code in php.
At the moment, I tried to create a new "Page" in my admin console. And then write some php-code inside. However, all my php code gets commented and since not executed.
I don't think that installing plugins such as Exec-PHP is a good idea, so I am trying to find other solutions.
Any comments/advice/suggestions how to make it?
I would be grateful if you give me some how-to link.
Thank you in advance.
You can use a custom page template in your theme for this and just keep the site empty in the admin panel:
http://codex.wordpress.org/Theme_Development#Custom_Page_Templates
By using Template tag's you can create custom page.
write this code on the top of your php file....
/*
Template Name: Your Template Name
*/

Wordpress plugin using existing theme

I am building a plugin for the first time and I have setup my rewrite rules to call a PHP file I have located in my plugin folder and this is working but all I have it output just now is "test"
I wanted to ask, is there is a way of pulling in the existing theme and using this as the basis for the page, then I can have my plugin just output the page content area so to speak.
My plugin is quite big in terms of the amount of data it handles so I would like to be able to use a menu link to the aforementioned file and this file can then output all the sub pages of content but still using the default theme (theme I have made) and fill the content area only is this possible and if so is there anything to explain this already available because I couldn't see it.
Any help with this would be much appreciated.
Edit: I have tried including a file from my current theme but this will give me a 500 error so I assume its not as simple as this.
Have you thought about using custom post types for your plugin content rather than relying on custom tables and separate code?
Other than that, you could use a shortcode (just one) and have users insert that into a regular WP page, the shortcode then displays all of your various plugin stuff.
It's hard to be more specific without understanding why you've done it this way.

wordpress use in own template

I've created an HTML page as part of my website which I would like to use as a template for news articles. The page has all the things it needs, it just needs to display the correct news article in it.
I installed WordPress on my webserver and now wonder how I can have wordpress publish articles using my HTML page?
Is this even possible since WordPress works with php?
thanks
What you are talking about is themes. Wordpress allows you to create a theme for your installation so it displays using your html/css (more or less)
The way I do this is copy the default theme and make changes in there, and then you can switch to your new theme in your settings.
Edit: in addition, you can also load up wordpress on any page using:
include( '/path_to/wordpress_install/wp-load.php' );
This will allow you to use wordpress functionality on any page of your site.
just edited the single.php file by pasting the desired html code and leaving the loop code there where the text needed to show up.
didn't need to break up the page in a header, body and footer. Left out sidebar for now, but will put it back after I edit it properly.

Categories