Шорткод с массивом опций и условным отображением на основе опций - PullRequest
1 голос
/ 25 июня 2019

Я искал решение и не смог найти.

Я создал шорткод, который отображает социальную ссылку в зависимости от типа с следующим кодом:

function ns_social_buttons_dynamic($atts,$content,$tag) {

          $facebookUrl = get_option('ns-company-facebook');
          $messengerUrl = get_option('ns-company-messenger');

          if (!empty($facebookUrl)) {
              $fb = '<a class="btn-ns btn-ns-second m-1 bg-facebook" href="https://facebook.com/'.$facebookUrl.'" role="button" target="blank"><i class="fab fa-facebook-f"></i></a>';
          };
          if (!empty($messengerUrl)) {
              $msngr = '<a class="btn-ns btn-ns-second m-1 bg-messenger" href="https://m.me/'.$messengerUrl.'" role="button" target="blank"><i class="fab fa-facebook-messenger"></i></a>';
          }

          $nsSocial = $fb . $msngr;


          //get admin url
          $addLink = get_site_url( $blog_id, 'wp-admin/', $scheme );

          //default value of shortcodes shows all social buttons
          $values = shortcode_atts(array(
            'type'   => 'all',
            'output' => 'raw',
          ),$atts);  

          //social button based on type
          $output = '';
          if($values['type'] == 'all' and !empty($nsSocial)){
            $output = $nsSocial;
          }
          else if($values['type'] == 'facebook' and !empty($facebookUrl)){
            $output = $fb;
          }
          else if($values['type'] == 'messenger' and !empty($messengerUrl)){
            $output = $msngr;
          }
          else {
            $output = 'field is empty <a href=' . $addLink . 'admin.php?page=ns-settings' .' >add username</a>';
          }
          return $output;

}
add_shortcode( 'ns-social', 'ns_social_buttons_dynamic' );

Этот код работаетпросто отлично, например, [ns-socia type='facebook'] выведет правую кнопку.

Теперь мне нужно, чтобы шорткод, например, следующий за [ns-socia type='facebook' output='raw'], отображал только URL, а не кнопку.

Я попытался выполнить следующее:

//social button based on type
          $output = '';
          if($values['type'] == 'all' and !empty($nsSocial)){
            $output = $nsSocial;
          }
          else if($values['type'] == 'facebook' and $values['output'] == 'raw' and !empty($facebookUrl)){
            $output = $facebookUrl;
          }
          else if($values['type'] == 'messenger' and $values['output'] == 'raw' and !empty($messengerUrl)){
            $output = $messengerUrl;
          }
          else {
            $output = 'field is empty <a href=' . $addLink . 'admin.php?page=ns-settings' .' >add username</a>';
          }
          return $output;

Я не уверен, что правильно делаю с условными условными выражениями php ...

...