Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Created December 16, 2024 17:11
Show Gist options
  • Select an option

  • Save nathaningram/2bce69bd359f5d1ced731475ff4ad7af to your computer and use it in GitHub Desktop.

Select an option

Save nathaningram/2bce69bd359f5d1ced731475ff4ad7af to your computer and use it in GitHub Desktop.

Revisions

  1. nathaningram created this gist Dec 16, 2024.
    38 changes: 38 additions & 0 deletions dynamic-copyright.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    //////////////////////////////////////////////////////////////////////////////
    // Dynamic Copyright Date Based on Start Date and Biz Name
    // Usage: [bww-copyright]
    //////////////////////////////////////////////////////////////////////////////


    // Shortcode to render a dynamic copyright statement
    function bww_copyright_shortcode() {
    // Retrieve the start year and business name from the Meta Box custom fields
    $all_options = get_option('site-info');
    $start_year = $all_options['bww_info_start_date'] ?? '';
    $business_name = $all_options['bww_info_business_name'] ?? '';

    // Validate business name
    if (empty($business_name)) {
    $business_name = __('Your Business Name', 'bww_copyright'); // Default fallback
    }

    // Validate start year
    $output_start_year = '';
    if (!empty($start_year) && is_numeric($start_year) && strlen($start_year) === 4) {
    $output_start_year = (int)$start_year;
    }

    // Current year
    $current_year = date('Y');

    // Generate the copyright statement
    $copyright = '© Copyright ';
    $copyright .= $output_start_year ? $output_start_year . '-' : '';
    $copyright .= $current_year . ' ';
    $copyright .= esc_html($business_name) . '. All Rights Reserved.';

    return $copyright;
    }

    // Register the shortcode
    add_shortcode('bww-copyright', 'bww_copyright_shortcode');