Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 90
  • Last Modified:

Word Press / php

Can I put custom php code into the body of a WordPress page?

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

?> in there?
0
Richard Korts
Asked:
Richard Korts
  • 5
  • 5
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Jason C. LevineNo oneCommented:
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
 
Richard KortsAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jason C. LevineNo oneCommented:
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
 
Richard KortsAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
Richard KortsAuthor Commented:
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
 
Richard KortsAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
Richard KortsAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
Beer can wait till tonight.

It can, but it shouldn't.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now