# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: schema, howto, shortcode
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/seo-howto-shortcode/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "How-To Steps Shortcode With HowTo Schema" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
