0

I have some custom PHP that creates a shortcode. I must have some errant code in here because it is showing up on the backend (edit side) of Wordpress. I am using the Divi theme and have it in a Text module with a class "SearchBarArea". So that is the name of the DIV it should be in, but on the front end, it is NOT in that DIV it is coming in right after "entry-content". Any idea?

function searchbar_function() {//Creates shortcode [searchbar]
$MainCategories = get_categories( array(
    'orderby' => 'name',
    'parent'  => 0
) );//Grabs just the parent categories
?>
<form action="action_page.php">
<?php 
foreach ( $MainCategories as $MainCategory ) {
    $MainCatID=esc_html($MainCategory->term_id);//Gets Main Category ID
    if($MainCatID!=='1' && $MainCatID!=='153'){//Exclude Uncategorized or Featured from Main Category List
    $MainCatURL=esc_url( get_category_link( $MainCategory->term_id ) );//Gets Main Category URL
    $MainCatName=esc_html( $MainCategory->name );//Gets Main Category Name
    $MainCatSlug=esc_html( $MainCategory->slug );//Gets Main Category Slug
    $MainCatCount=esc_html( $MainCategory->category_count );
    $MainCat='<label>'.$MainCatName.'</label>';
        echo $MainCat;
    $MainCatCode='<option value="'.$MainCatSlug.'">ALL '.$MainCatName.'</option>';?>
        <select name="<?php echo $MainCatSlug?>-dropdown"> 
    <option value=""><?php echo esc_attr_e( 'Select:', 'textdomain' ); ?></option> 
    <?php 
        echo $MainCatCode;//Makes Main Category first choice
    $categories = get_categories( array( 'child_of' => $MainCatID ) );//Number is the Main Category ID 
    foreach ( $categories as $category ) {
        $SubCatValueName= esc_attr($category->category_nicename);
        $SubCatValue= esc_attr( '/category/archives/' . $category->category_nicename );
        $SubCatName=esc_html( $category->cat_name );
        $SubCatCount=esc_html( $category->category_count );
        $SubCatOption ='<option value="'.$SubCatValueName.'">'.$SubCatName.'</option>';
        echo $SubCatOption;
    }
    ?>
        </select>
        <?php
    }
}?>
<input type="submit" value="Search">
        </form>
<?php
}
add_shortcode( 'searchbar', 'searchbar_function' );
1

Even with the missing open php tag, this won't work the way you're trying to do it. The Divi code module accepts html code and PHP shortcodes. I haven't tried javascript, so not sure about that.

You'll need to integrate your code as a stand alone plugin. After that, you can use the shortcode in the Divi code module and should also be able to use it in the text tab mode.

  • This IS a stand alone plugin. The <php and ?> start and end tags are in there (I just didn't include them in my code snippet). I thought I might be able to fix it by making it a plugin. Still not working correctly. What is wrong with the way I am doing it? I am not a PHP guru by any means. I typically do a tremendous amount of trial and error from stuff I find online to build what I need. – Kirk Jan 8 '17 at 15:40
1

I figured it out.I rearranged the HTML and used variables so there was no break in the code. I also removed

esc_attr_e( 'Select:', 'textdomain' );

Because it was causing the results to appear outside the DIV the shortcode was put in.

function searchbar_function() {//Creates shortcode [searchbar]
$MainCategories = get_categories( array(
    'orderby' => 'name',
    'parent'  => 0
) );//Grabs just the parent categories
$CodeResult.='<form action="action_page.php">';

foreach ( $MainCategories as $MainCategory ) {
    $MainCatID=esc_html($MainCategory->term_id);//Gets Main Category ID
    if($MainCatID!=='1' && $MainCatID!=='153'){//Exclude Uncategorized or Featured from Main Category List
    $MainCatURL=esc_url( get_category_link( $MainCategory->term_id ) );//Gets Main Category URL
    $MainCatName=esc_html( $MainCategory->name );//Gets Main Category Name
    $MainCatSlug=esc_html( $MainCategory->slug );//Gets Main Category Slug
    $MainCatCount=esc_html( $MainCategory->category_count );
    $MainCat='<label>'.$MainCatName.'</label>';
    $MainCatCode='<option value="'.$MainCatSlug.'">ALL '.$MainCatName.'</option>';
    $CodeResult.=$MainCat;
    $SelectCode='<select name="'.$MainCatSlug.'-dropdown"><option value="">Select:</option>';
        $CodeResult.=$SelectCode;
        $CodeResult.=$MainCatCode;//Makes Main Category first choice
    $categories = get_categories( array( 'child_of' => $MainCatID ) );//Number is the Main Category ID 
    foreach ( $categories as $category ) {
        $SubCatValueName= esc_attr($category->category_nicename);
        $SubCatValue= esc_attr( '/category/archives/' . $category->category_nicename );
        $SubCatName=esc_html( $category->cat_name );
        $SubCatCount=esc_html( $category->category_count );
        $SubCatOption ='<option value="'.$SubCatValueName.'">'.$SubCatName.'</option>';
        $CodeResult.=$SubCatOption;
    }
        $CodeResult.='</select>';
    }
}
$SubmitEnd='<input type="submit" value="Search">
        </form>';
$CodeResult.=$SubmitEnd;
    return $CodeResult;
}
add_shortcode( 'searchbar', 'searchbar_function' );
0

Use ob_start() before starting your shortcode codes and right after the shortcode codes use something like

$my_clean_ob = ob_get_clean();
return $my_clean_ob;

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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