Search code examples
phpwordpressshortcode

Create a Wordpress Shortcode with functions in functions.php


The shortcode I'm trying to create should return the 1 Hungarian word described below, this should change month to month. The code has been placed into functions.php and returns nothing when the shortcode is used in a page or post. Any help would be much appreciated. It does work when used as simple php and html by using echo honapnev($honap);.

    $honap = date("n");
function honapnev_shortcode( $ho ) {

  if ($ho==1) {
        return "Januári";
    }
  elseif ($ho==2) {
        return "Februári";
    }
  elseif ($ho==3) {
        return "Márciusi";
    }
  elseif ($ho==4) {
        return "Áprilisi";
    }
  elseif ($ho==5) {
        return "Májusi";
    }
  elseif ($ho==6) {
        return "Júniusi";
    }
  elseif ($ho==7) {
        return "Júliusi";
    }
  elseif ($ho==8) {
        return "Augusztusi";
    }
  elseif ($ho==9) {
        return "Szeptemberi";
    }
  elseif ($ho==10) {
        return "Októberi";
    }
  elseif ($ho==11) {
        return "Novemberi";
    }
  elseif ($ho==12) {
        return "Decemberi";
    }
}
add_shortcode( 'honapnev', 'honapnev_shortcode' );

When I remove the line add_shortcode( 'honapnev', 'honapnev_shortcode' ); the shortcode in brackets appears on the page...


Solution

  • function honapnev_shortcode($atts)
    {        
        switch (date("n"))
        {
            case 1:
                {
                    $month="Januári";
                    break;
                }
            case 2:
                {
                    $month="Februári";
                    break;
                }
            case 3:
                {
                    $month="Márciusi";
                    break;
                }
            case 4:
                {
                    $month="Áprilisi";
                    break;
                }
            case 5:
                {
                    $month="Májusi";
                    break;
                }
            case 6:
                {
                    $month="Júniusi";
                    break;
                }
            case 7:
                {
                    $month="Júliusi";
                    break;
                }
            case 8:
                {
                    $month="Augusztusi";
                    break;
                }
            case 9:
                {
                    $month="Szeptemberi";
                    break;
                }
            case 10:
                {
                    $month="Októberi";
                    break;
                }
            case 11:
                {
                    $month="Novemberi";
                    break;
                }
            case 12:
                {
                    $month="Decemberi";
                    break;
                }
                default:
                {
                    $month="Januári";
                    break;
                }
        }
        return $month;
    }
    
    add_shortcode('honapnev', 'honapnev_shortcode');
    

    or

    function honapnev_shortcode($atts)
    {        
       $months=array("Januári", "Februári", "Márciusi", "Áprilisi", "Májusi","Júniusi", "Júliusi", "Augusztusi","Szeptemberi", "Októberi", "Novemberi", "Decemberi");
       return $months[date("n")-1];
    }
    
    add_shortcode('honapnev', 'honapnev_shortcode');