PHP code in wordpress page - php

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

Related

Adding Monarch plugin to stand alone PHP script emulating WordPress layout

I'm putting together a WordPress site for a charity organization which upgrades the look & feel of their existing site. Their existing site had a few PHP scripts and an events database for their project listings and I changed the output so that the script emulates the WordPress UI using:
define( 'WP_USE_THEMES', true );
require_once "wp-load.php";
The site uses the Monarch social plugin to enable sharing and I need to have that work on the emulated project page.
I haven't been able to find what I should include.
I tried adding both of these lines, separately, but they had no affect:
require_once "wp-content/plugins/monarch/monarch.php";
require_once "wp-content/plugins/monarch/core/init.php";
Upon looking at the code and trying to call functions that looked like potential functions to execute it, they resulted in an error or does nothing.
As someone completely unfamiliar with the intricacies of WordPress doing a pro bono favor for a charity organization, I did not realize that page templates could be used as the destination for custom PHP code accessing a non-WordPress database, etc.
The solution steps (which may hopefully help someone else equally unaware of WordPress) are as follows:
Use the page template page as reference to create a page template.
I added my page template to the same location as the theme's page.php template
Then create a page in WordPress and specify its template as the one just created.
Call that new page with whatever parameters after ?xyz=value;abc=etc. as needed
This may be basic for people who live in WordPress but it wasn't immediately clear to me.
Thanks to the comments by #ChrisHaas!

WordPress /wp-inlcludes/functions.php: Custom functions/code disappears

I have added two custom written functions to my wordpress code.
One of them uses the add_action() and the other uses the add_filter() function of wordpress.
After deploying the code, both functions work as expected, but after some time (I am not sure how long it takes and what causes wordpress to do so) the code just magically disappears from the file.
I am now wondering
Why is this happening?
What can I do so that this won't happen again?
Thanks for your help
Hey you just mentioned that you've added code into /wp-includes/functions.php file. You've used action and filters that's good but your code should be go into active theme's (child theme is the best practice) functions.php file. Files under /wp-admin/ and /wp-includes/ get updated automatically from WP when new update is available (based on settings you have done on your WP setup you can ignore updates as well).
So, your code must be removed by update. Files were overridden by updates. If you have backup copy those code and put it into active theme's functions.php file /wp-content/themes/yourtheme/functions.php is the correct file where you can add the code.
If you have added code into theme's functions.php file make sure you didn't get theme updated otherwise you may lost your work as well that's the reason you can use the Child theme. You can learn how to create child theme here: https://developer.wordpress.org/themes/advanced-topics/child-themes/
Why is this happening?
Most probably because someone updated WordPress to latest version. Before you are going to tell me "but I did not press anything, ever!" please bear in mind, that WordPress updates itself automatically. Especially in the newer versions.
Here is nice article about WordPress automatic updates. It also describes how to turn this off, but I do not recommend doing so.
What can I do so that this won't happen again?
Develop custom WordPress plugin, containing your customizations. Here is nice step by step tutorial
you shouldn't make any changes into wp-includes and wp-admin folders, these folders will update automatically if your wordpress is updated. so if you want to make any changes using hooks and actions you can place the code into your current theme's functions.php. that will work as expected and your code won't disappear.

How to save PHP code written in Wordpress page

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

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.

Simple way to add "the loop" from Wordpress into any html/php template (that is not made for wordpress)

im looking easy steps for convert any html/php template into a very simple wordpress theme for personal use. I have a site, in there i want news at the begining using the wordpress interface, i worked a lot with wordpress and i made some themes, but modifing already made ones, so i didnt care a lot about the code.
So, lookin in internet i dint found a good step by step tutorial in order to create a simple theme in wordpress from 0. Remember that i dont need sidebars, widgets and soo, i only need "the loop", or, the last 3 post made.
It's as simple as installing wordpress correctly and then including the wp-blog-header file into your pages. Doing so will give you access to all of WP's functions.
require('/path/to/wp-blog-header.php');
$posts = get_posts('arguments');

Categories