Home / Archive / Dynamic Copyright & Current Year Shortcodes
Duplicate Snippet

Embed Snippet on Your Site

Dynamic Copyright & Current Year Shortcodes

Add this snippet to the functions.php file in your theme or plugin. Compatible with both classic and block themes.
The shortcodes will execute live in the block editor.

Type [year] in any text block to return the current year.
Type [copyright] in any text block to return the name of your website + the copyright symbol and current year.

Use with the code snippet that enables shortcode execution in text:

// === Allow shortcodes in classic contexts ===
add_filter( 'widget_text', 'do_shortcode' );
add_filter( 'the_content', 'do_shortcode' );

// === Force shortcodes to render in block content (Site Editor too) ===
add_filter( 'render_block', function( $block_content, $block ) {
return do_shortcode( $block_content );
}, 10, 2 );

Example output: "My Website Title © 2024."

Code Preview
php
<?php
<?php
add_action( 'init', function() {
    // === Shortcodes ===
    function afl_copyright_shortcode() {
        $site_name = get_bloginfo( 'name' );
        return "<span class='site-title'>" . esc_html( $site_name ) . "</span>" .
               "<span class='copyright'> Copyright &copy; " . date( 'Y' ) . "</span>";
    }
    add_shortcode( 'copyright', 'afl_copyright_shortcode' );
    function afl_year_shortcode() {
        return "<span class='date year'>" . date( 'Y' ) . "</span>";
    }
    add_shortcode( 'year', 'afl_year_shortcode' );
    // === Dynamic Block: Copyright ===
    register_block_type( 'afl/copyright', array(
        'render_callback' => 'afl_copyright_shortcode',
        'attributes'      => array(),
        'title'           => __( 'Copyright', 'afl' ),
        'category'        => 'widgets',
        'icon'            => 'admin-site-alt3',
        'description'     => __( 'Displays site name and copyright year.', 'afl' ),
        'supports'        => array( 'html' => false )
    ) );
    // === Dynamic Block: Year ===
    register_block_type( 'afl/year', array(
        'render_callback' => 'afl_year_shortcode',
        'attributes'      => array(),
        'title'           => __( 'Current Year', 'afl' ),
        'category'        => 'widgets',
        'icon'            => 'calendar',
        'description'     => __( 'Displays the current year.', 'afl' ),
        'supports'        => array( 'html' => false )
    ) );
});
// === Inject CSS automatically ===
add_action( 'wp_enqueue_scripts', function() {
    $custom_css = "
        .site-title {
            font-weight: 600;
            margin-right: 0.3em;
        }
        .copyright {
            color: currentColor;
            font-size: 0.9em;
        }
        .date.year {
            font-weight: 500;
        }
    ";
    wp_add_inline_style( 'wp-block-library', $custom_css );
});

Comments

Add a Comment