Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to create a form that I can use a shortcode to insert into my site.

It would be really nice if I could create the HTML part in a seperate file and then insert it with a PHP shortcode (to seperate the logic of the page from the mechanics of making it into a shortcode).

How could I do this?

-- Update --

This is what I've done: I have two files. One called 'profiletemplate.php' and one called 'scodes'. They are both part of a plugin that I am making for my site with an init.php that initializes them. Here is their content:

init.php

<?php
require_once(dirname(__FILE__).'/pages/scodes.php');
?>

scodes.php

function jf_testcode() {
    include dirname(__FILE__) . 'profiletemplate.php';
}

add_shortcode('testfield', 'jf_testcode');

profiletemplate.php

<?php // Template for my form shortcode ?>
<form>
Testing
</form>

I then use the [testfield] shortcode on a page in my site.

Update 2

So this method is working, but it isn't inserting the HTML where the shortcode is called. Instead it is just inserting the content at the top of the page (like if I said 'echo 'Testing'' instead of 'return 'Testing'' in a function.

share|improve this question
    
Well, for starters, you CAN write plain HTML in a PHP file. You then have to write a function in your functions.php that requires/includes this certain file, and by means of add_shortcode() set up your desired shortcode to call your function. –  t f Mar 8 '13 at 13:12

2 Answers 2

up vote 2 down vote accepted

Something I forgot in my previous comment was that shortcodes return content, both the suggested include and my alternative get_template_part will directly output the content (which is what you are seeing with the content appearing at the top of your page instead of where the shortcode is called). To counteract this we must use output buffering.

Define the shortcode in your functions.php (or your site's site-specific functions file).

function my_form_shortcode() {
    ob_start();
    get_template_part('my_form_template');
    return ob_get_clean();   
} 
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );

Then in your theme folder you need a file called my_form_template.php which will be loaded anywhere you place the shortcode.

share|improve this answer
    
Perfect! This works fabulously! –  William Mar 8 '13 at 16:34

Add the following to your functions.php:

function my_form_shortcode() {
    include dirname( __FILE__ ) . 'my_form_template.php';
} // function my_form_shortcode
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );

File my_form_template.php:

<?php // Template for my form shortcode ?>
<form ...>
    FIELDS
</form>
share|improve this answer
1  
You could also use get_template_part() inside your shortcode function. –  helgatheviking Mar 8 '13 at 13:21
    
I tried implementing @t f's solution - I added an update to my post about it –  William Mar 8 '13 at 14:46
    
@helgatheviking - how does that function work? –  William Mar 8 '13 at 14:47
    
See the codex for a description of get_template_part() and see my answer for how to use it in conjunction with your shortcode. –  helgatheviking Mar 8 '13 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.