2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator says

Hi, I have a contact form shortcode which looks like this:

function contact_form_shortcode($params = array()) {
    extract(shortcode_atts(array(
        'fmail' => '#',
        'thanks' => 'Thank You - we will respond shortly.'

    ), $params));
    ob_start();
    include(get_theme_root() . '/' . get_template() . "/functions/contact_form.php");

    return ob_get_clean();
}

add_shortcode('contact_form', 'contact_form_shortcode');

For all of my shortcodes I am using a clean up method which looks like this:

function parse_shortcode_content( $content ) {

   /* Parse nested shortcodes and add formatting. */
    $content = trim( do_shortcode( shortcode_unautop( $content ) ) );

    /* Remove '' from the start of the string. */
    if ( substr( $content, 0, 4 ) == '' )
        $content = substr( $content, 4 );

    /* Remove '' from the end of the string. */
    if ( substr( $content, -3, 3 ) == '' )
        $content = substr( $content, 0, -3 );

    /* Remove any instances of ''. */
    $content = str_replace( array( '<p></p>' ), '', $content );
    $content = str_replace( array( '<p>  </p>' ), '', $content );

    return $content;
}

// move wpautop filter to AFTER shortcode is processed

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);
add_filter( 'the_content', 'shortcode_unautop',100 );

While with all of the shortcodes it works great, with the contact form I get an ‘unexpected token <’ I am not sure how should I run the parse_shortcodes_content function while I am using ob_start().

The simplest solution would be not too use the echo buffer (ob_start) but I am not sure how to use the include statement without printing out the shortcode always at the top of the page.

2178 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator says

Any idea how to use include inside a shortcode without using the ob_start()... ?

973 posts
  • United Kingdom
  • Attended a Community Meetup
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 5 000 and 10 000 dollars
  • Has been a member for 3-4 years
  • Envato Studio (Microlancer) Beta Tester
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • Exclusive Author
aaranmcguire says
if you turn everything in the contact_form.php to a return, not echo then you could do
$code = include($path . 'contact_form.php');
$code = str_replace('<p></p>', '', $code);

I think anyway, not used this method ever but seen it done. if not, skype me: aaran.mcguire

303 posts Codestar
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Has been a member for 5-6 years
  • Envato Studio (Microlancer) Beta Tester
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
+2 more
Codestar says
ob_start();
include(get_theme_root() . '/' . get_template() . "/functions/contact_form.php");
return ob_get_clean();

can be used directly include. but if so you can’t use fmail, thanks params. just try it for error message. you can understand where is problem ob_start or not :)

return include(get_theme_root() . '/' . get_template() . "/functions/contact_form.php");

by the way, why you using an include there ? write your codes directly ? and add some code of contact_form.php and write all error line. just this “unexpected token” ?

by
by
by
by
by
by