Solved

Word Press / php

Posted on 2015-02-08
11
78 Views
Last Modified: 2015-02-09
Can I put custom php code into the body of a WordPress page?

For example, see attached. Can I put <?php
................

?> in there?
0
Comment
Question by:Richard Korts
  • 5
  • 5
11 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40597478
You can put whatever you want in a Wordpress page.  However... a page in Wordpress is made up of quite a few different PHP files.  Some are for the basic Wordpress operation and others are for the theme which is supposed to be the 'visual' presentation.  Unless you already know which file you need to modify, you need to do some research before you change anything.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40597551
Yes, you can just not via the visual editor (without adding a plugin that isn't rcommended for security reasons).

The way to do it is to make a new page template that is specific to the slug or ID of the page you want to alter.  Copy page.php from your theme to page-the-slug.php or page-idnumber.php in the theme folder and then edit it.  Add your custom PHP code wherever you wish.

Caveat: you can't do this if you want to put the php in the middle of the content saved via the WordPress editor (e.g. Content-code-content) because the content is called in with a single function (the_content() ) and you can't specify any breaks.  If you do want that pattern, you are better off taking the content out of the editor completely and using a page template as described above to hold both the content and your custom code.  Makes editing harder, but you get more control.

Ref: http://codex.wordpress.org/Template_Hierarchy
0
 

Author Comment

by:Richard Korts
ID: 40598728
To Jason Levine & Dave Baldwin,

This is long winded, but this relates specifically to this question & the one about adding database tables.

The customer has a WordPress site; I was brought in after it was 90% complete. Although I am somewhat of a novice, I can do basic WordPress.

They want to add a capability to allow their customers to sign up for service contracts. It is supposed to proceed like this. This design is more or less "cast in concrete".

On a page that is mostly text, it asks for the customer zip code; my thinking is a simple form with that as a text field & a "Go" button (Submit).

It then looks up the zip code in a database table with custom php. If found it continues to step 2. If not found, it displays a simple form page with some text asking the customer for their Name, phone # &  email address & telling them they will be contacted regarding details of their request.

Step 2 is another form with text, asking them to select "# of Zones" from a pulldown (Select) list. the choices are 1 - 19, 20 or more and "don't know". If they pick 20 or more or "don't know", they are taken to a page that displays a simple form page with some text asking the customer for their Name, phone # &  email address & telling them they will be contacted regarding details of their request. That emails to the company

If zones is 1 - 19, there is a database table that lists pricing for 3 plans, Bronze, Silver & Gold that has a different row for each # of zones. A page is displayed that shows the price for each. They pick the one they want & it takes them to a payment page that asks for the normal stuff (Name, Address, Credit Card #, Expiration date, email address, etc.).

The processing after that is the normal kind of thing, we will probably use Authorize.net. Thank You page, failure page, etc.

There are a few other aspects I have skipped, if I figure out the basics, as just explained, I think I can do the rest.

Of course if I could do all this with plain php / MySQL, I can do it all today, but I want all the pages to have the Theme, menus, footer., etc.

So I thought I could start with a form page (built by me, no plugin, plain HTML) for step one; they click the "Go" button, the action on the form is a custom php page (built by me) that just looks up the zip code in the corresponding table, & redirects to 1 of 2 WordPress pages depending. The "failure" one is a Contact Form 7, they enter the info, it submits an email to the company. The other one is another form with some text & a select form field with the # of zones. They select that, similar processing, except now I have to pass the pricing info for Bronze, Silver & Gold to a WP page that has php embedded, I was thinking of a session variables to pass the pricing (e.g., $_SESSION['bronze_price'], etc.

That's essentially what I need to do.

Comments & suggestions based on your expert experience in this area would be greatly appreciated.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40598741
So do all of this the way I suggest.  Make new, blank WordPress pages and create matching page templates in the theme with your custom HTML and PHP in those templates.  We do this all the time when the need goes beyond a simple plugin.

From above, it sounds like you will need this code snippet in functions.php:

add_action('init', 'myStartSession', 1);
function myStartSession() {
    if(!session_id()) {
        session_start();
    }
}

Open in new window


to enable session handling in WordPress.  Beyond that, you should be all set if you follow my suggestions above.
0
 

Author Comment

by:Richard Korts
ID: 40598778
To Jason C. Levine,

Thanks a lot.

I don't understand what this means:

The way to do it is to make a new page template that is specific to the slug or ID of the page you want to alter.  Copy page.php from your theme to page-the-slug.php or page-idnumber.php in the theme folder and then edit it.  Add your custom PHP code wherever you wish.

None of the pages to do this yet exist. I don't know what slug means. Does page-the-slug.php exist or am I making that?

Thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 40598795
1) Make a new page.  Give it the title "rkorts test" in the visual editor.  Do not add content.  Publish.

2) The slug for the page should be "rkorts-test"

3) Copy page.php to page-rkorts-test.php in the theme folder.

4) Edit page-rkorts-test.php in Appearance | Editor (or whatever you prefer).  Locate the function the_content() (or get_the_content() ) and replace that PHP with:  <?php echo 'I am a test'; ?>

5) Open beer, enjoy enlightenment.
0
 

Author Comment

by:Richard Korts
ID: 40598905
To Jason C. Levine,

I cannot find page.php in WordPress. The theme is called "Bee". If I use ftp, I can find page.php in the bee folder on the server. Is that where I have to "copy page.php to page-rkorts-test.php"?

Not in WordPress?

Thanks
0
 

Author Comment

by:Richard Korts
ID: 40598932
To Jason C. Levine,

I found page.php (in WordPress), can't see any way to copy it (in WordPress).

There is no the_content() or get_the_content() in page.php. See attached.

Thanks
page.php
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40598988
I found page.php (in WordPress), can't see any way to copy it (in WordPress).

Can't (in WordPress).  Use FTP.

There is no the_content() or get_the_content() in page.php. See attached.

This theme is using nested templates.  Relevant code block is:

 <?php 
                            if( ux_custom_meta( 'pagebuilder_switch' ) == 'switch_pagebuilder' ) {
                                
                                ux_show_module();
                                
                            } else {
                                
                                get_template_part('single','template');
                                
                            }
                            ?>

Open in new window


So you can just replace that block with your own PHP/HTML to get it to display.  The get_template_part() function calls to single.php.  If you look at single.php it probably has a case:switch or if-then-elseif block that does slightly different things depending on which container calls it.  That's neither here nor there, though as we are looking to not deal with it.

The caveat is the presence of ux_show_module() as that is not a core WP function so it's something being added on by the theme.  I assume it's some kind of visual page builder thingy but if removing it breaks everything, we'll need to account for that.
0
 

Author Closing Comment

by:Richard Korts
ID: 40599033
Perfect. That does shed a LOT of light. Beer can wait till tonight.

I still don't understand about the database (how to add tables), but I'll try to research further.

Thanks
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40599349
Beer can wait till tonight.

It can, but it shouldn't.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SASS Files in Wordpress 1 39
Add and Edit User Meta Data 6 37
Encode a password 2 39
Connect MySql database to wordpress site 3 20
Who says nothing in life is free? WordPress.com is a freebie. WordPress.org's downloadable publishing platform is free. Heck, even WordPressMU is free. WordPress is an open source project, which means it can be used on any personal or commerc…
I want to start by talking about the use of plug-ins for WordPress. I started a web-site for a company I was working for a few years ago; I had extremely basic knowledge of HTML. I am a Graphic Designer by trade so I invited the opportunity as a cha…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now