Halloween Sale 15% off Ends in See the plans
PHP

How-To Steps Shortcode With HowTo Schema

Adds a [howto] shortcode that wraps a numbered list of steps, keeps it on the page as normal content and outputs matching HowTo structured data.

Don’t have WPIDE yet? Install it on your site

Code

add_shortcode( 'howto', function ( $atts, $content = '' ) {
    $atts    = shortcode_atts( array( 'name' => '', 'time' => '' ), $atts, 'howto' );
    $content = do_shortcode( (string) $content );

    if ( ! preg_match_all( '#<li[^>]*>(.*?)</li>#is', $content, $matches ) ) {
        return $content;
    }

    $steps = array();

    foreach ( $matches[1] as $i => $step ) {
        $text = trim( wp_strip_all_tags( $step ) );

        if ( '' !== $text ) {
            $steps[] = array( '@type' => 'HowToStep', 'position' => $i + 1, 'text' => $text );
        }
    }

    $schema = array(
        '@context' => 'https://schema.org',
        '@type'    => 'HowTo',
        'name'     => $atts['name'] ? wp_strip_all_tags( $atts['name'] ) : wp_strip_all_tags( get_the_title() ),
        'step'     => $steps,
    );

    if ( absint( $atts['time'] ) ) {
        $schema['totalTime'] = 'PT' . absint( $atts['time'] ) . 'M';
    }

    return '<div class="howto">' . $content . '</div><script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . '</script>';
} );

Notes

Wrap a numbered list in [howto name="How to repot a plant" time="20"] ... [/howto]; Google no longer shows HowTo rich results, but other search and AI tools read the markup.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.