Skip to content

PHP Template Usage

Render the like button directly from PHP template files.

Overview

If you are building a custom theme or modifying template files, you can render the like button directly from PHP without using the shortcode. Simple Post Like provides a clean public helper function — spl_like_button() — that wraps the LikeButton class and gives you full control over placement and style from within your theme’s template files.


The Helper Function

php

spl_like_button( $post_id, $style );

Parameters

ParameterTypeDefaultDescription
$post_idintget_the_ID()The post to render the button for
$stylestringglobal settingDisplay style — button_default, icon_counter, or icon_only

Return Value

Returns the like button HTML as a string. Use echo to output it.


Basic Usage

Call inside the loop with no arguments to render the button for the current post using the global display style from Settings:

php

<?php echo spl_like_button(); ?>

With a Specific Style

Override the display style for this instance only. This maps directly to the $override_style parameter on LikeButton::get_like_button_html() — when provided, it takes precedence over the saved Settings value:

php

<?php echo spl_like_button( get_the_ID(), 'icon_counter' ); ?>
<?php echo spl_like_button( get_the_ID(), 'icon_only' ); ?>
<?php echo spl_like_button( get_the_ID(), 'button_default' ); ?>

If you pass an empty string or omit the $style argument, the global display style from Settings → Simple Post Like → Display Style is used.


Capture as a Variable

Since the function returns a string, capture it into a variable and use it anywhere:

php

$like_button = spl_like_button( get_the_ID(), 'icon_counter' );
echo '<div class="post-actions">' . $like_button . '</div>';

Usage in Template Files

single.php

Render the button after post content on single post pages:

php

<?php while ( have_posts() ) : the_post(); ?>
    <article>
        <?php the_content(); ?>
        <footer class="post-footer">
            <?php echo spl_like_button(); ?>
        </footer>
    </article>
<?php endwhile; ?>

archive.php / index.php

Render a compact icon button after each post excerpt on archive pages:

php

<?php while ( have_posts() ) : the_post(); ?>
    <article>
        <?php the_excerpt(); ?>
        <div class="post-actions">
            <?php echo spl_like_button( get_the_ID(), 'icon_counter' ); ?>
        </div>
    </article>
<?php endwhile; ?>

content.php (template part)

Render the button inside a template part:

php

<footer class="entry-footer">
    <?php echo spl_like_button( get_the_ID(), 'button_default' ); ?>
</footer>

A custom related posts loop

Render a like button for a specific post ID outside the main loop:

php

<?php
$related_posts = get_posts( [
    'numberposts' => 3,
    'category'    => get_the_category()[0]->term_id,
    'exclude'     => [ get_the_ID() ],
] );

foreach ( $related_posts as $related ) : ?>
    <div class="related-post">
        <h4><?php echo esc_html( $related->post_title ); ?></h4>
        <?php echo spl_like_button( $related->ID, 'icon_only' ); ?>
    </div>
<?php endforeach; ?>

How the Style Parameter Works

Under the hood, spl_like_button() calls:

php

LikeButton::instance()->get_like_button_html( $post_id, $override_style );

The $override_style parameter is typed as ?string — when null is passed (no style argument), LikeButton falls back to the saved display_type setting from the database. When a valid style string is passed, it is used directly, bypassing the setting entirely.

This means per-instance style overrides in templates behave exactly the same as the style attribute on the shortcode.


Checking if Simple Post Like is Active

If you are writing code that should work whether or not the plugin is installed — for example, in a theme that optionally supports Simple Post Like — wrap the call in a function_exists() check:

php

<?php if ( function_exists( 'spl_like_button' ) ) : ?>
    <?php echo spl_like_button(); ?>
<?php endif; ?>

Auto Injection and Template Usage

If you call spl_like_button() directly in a template file and auto injection is also enabled, the button may appear twice on that page. To avoid this, either:

  • Disable auto injection in Settings → Simple Post Like → Auto Injection, or
  • Set the single post injection position to None and use template functions for precise manual placement

Next Steps

On This Page