Skip to content

Instantly share code, notes, and snippets.

@humayunahmed8
Last active October 19, 2023 06:48
Show Gist options
  • Save humayunahmed8/38c29000d4024c1b0189d8e0e2aecb78 to your computer and use it in GitHub Desktop.
Save humayunahmed8/38c29000d4024c1b0189d8e0e2aecb78 to your computer and use it in GitHub Desktop.
Conditional url type selector with codestar metabox framework
<?php
function lhcorp_section_banner_two_metabox($metaboxes){
$section_id = 0;
if ( isset( $_REQUEST['post'] ) || isset( $_REQUEST['post_ID'] ) ) {
$section_id = empty( $_REQUEST['post_ID'] ) ? $_REQUEST['post'] : $_REQUEST['post_ID'];
}
if('section'!=get_post_type($section_id)){
return $metaboxes;
}
$section_meta = get_post_meta($section_id,'lhcorp-section-type',true);
if(!$section_meta){
return $metaboxes;
}else if('banner-two' != $section_meta['section-type'] ){
return $metaboxes;
}
$metaboxes[] = array(
'id'=>'lhcorp-section-banner-two',
'title'=>__('Banner Two Settings','lhcorp'),
'post_type'=>'section',
'context'=>'normal',
'priority'=>'default',
'sections'=>array(
array(
'name'=>'lhcorp-section-type-banner-two',
'icon'=>'fa fa-image',
'fields'=>array(
array(
'id'=>'button-label',
'type'=>'text',
'title'=>__('Button Label','lhcorp'),
'default'=>__('Learn More','lhcorp')
),
array(
'id' => 'button-url-type',
'type' => 'select',
'title' => __('Button URL Type', 'lhcorp'),
'options' => array(
'new_tab' => __('Custom URL', 'lhcorp'),
'same_tab' => __('WordPress Page', 'lhcorp'),
),
),
array(
'id' => 'button-link-wp-page',
'type' => 'select',
'title' => __('Select WordPress Page', 'lhcorp'),
'options' => lhcorp_get_wp_pages_options(), // You need to implement the function lhcorp_get_wp_pages_options()
'dependency' => array('button-url-type', '==', 'same_tab'),
),
array(
'id' => 'button-link-custom-url',
'type' => 'text',
'title' => __('Custom URL', 'lhcorp'),
'default'=>__('#','lhcorp'),
'dependency' => array('button-url-type', '==', 'new_tab'),
),
)
)
)
);
return $metaboxes;
}
add_filter('cs_metabox_options','lhcorp_section_banner_two_metabox');
<?php
// Banner Button URL
$button_url_type = isset($lhcorp_section_meta['button-url-type']) ? $lhcorp_section_meta['button-url-type'] : 'same_tab';
if ('same_tab' === $button_url_type && !empty($lhcorp_section_meta['button-link-wp-page'])) {
// Use WordPress page URL
$button_url = get_permalink($lhcorp_section_meta['button-link-wp-page']);
// $link_target = '_self'; // Open in the same tab
} elseif ('new_tab' === $button_url_type && !empty($lhcorp_section_meta['button-link-custom-url'])) {
// Use custom URL
$button_url = esc_url($lhcorp_section_meta['button-link-custom-url']);
// $link_target = '_blank'; // Open in a new tab
} else {
// Default to a fallback URL or do something else based on your requirements
$button_url = '#';
// $link_target = '_self'; // Open in the same tab
}
// Check if the key 'button-label' exists in the array
$button_label = isset($lhcorp_section_meta['button-label']) ? esc_html($lhcorp_section_meta['button-label']) : '';
?>
<?php
// Conditionally check custom url value
// Link starts with '#' & '/' => '_self'
// Link starts with 'http' => '_blank'
if ('new_tab' === $button_url_type && !empty($lhcorp_section_meta['button-link-custom-url'])) {
// Use custom URL
$button_url = esc_url($lhcorp_section_meta['button-link-custom-url']);
// Check the value of $button_url to determine link behavior
if (strpos($button_url, '#') === 0 || strpos($button_url, '/') === 0) {
// Link starts with '#' or '/'
$link_target = '_self'; // Open in the same tab
} elseif (strpos($button_url, 'http') === 0) {
// Link starts with 'http'
$link_target = '_blank'; // Open in a new tab
} else {
// Default to a fallback URL or do something else based on your requirements
// $button_url = '#';
$link_target = '_self'; // Open in the same tab
}
} else {
// Default to a fallback URL or do something else based on your requirements
// $button_url = '#';
$link_target = '_self'; // Open in the same tab
}
?>
<a class="btn btn-primary" href="<?php echo esc_url($button_url); ?>" target="<?php echo esc_attr($link_target); ?>">
<?php echo $button_label; ?>
</a>
@humayunahmed8
Copy link
Author

button-type

@humayunahmed8
Copy link
Author

<?php 
      // **Without custom link value check. In that case, If select "WordPress Page" => "_self" / Custom URL => "_blank" / By Default "_self"**
      
      // Banner Button URL
      $button_url_type = isset($lhcorp_section_meta['button-url-type']) ? $lhcorp_section_meta['button-url-type'] : 'same_tab';

      if ('same_tab' === $button_url_type && !empty($lhcorp_section_meta['button-link-wp-page'])) {
          // Use WordPress page URL
          $button_url = get_permalink($lhcorp_section_meta['button-link-wp-page']);
          $link_target = '_self'; // Open in the same tab
      } elseif ('new_tab' === $button_url_type && !empty($lhcorp_section_meta['button-link-custom-url'])) {
          // Use custom URL
          $button_url = esc_url($lhcorp_section_meta['button-link-custom-url']);
          $link_target = '_blank'; // Open in a new tab
      } else {
          // Default to a fallback URL or do something else based on your requirements
          $button_url = '#';
          $link_target = '_self'; // Open in the same tab
      }	
      // Check if the key 'button-label' exists in the array
      $button_label = isset($lhcorp_section_meta['button-label']) ? esc_html($lhcorp_section_meta['button-label']) : '';
    ?>

@humayunahmed8
Copy link
Author

Screenshot_162

Button metabox with custom url link target select

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment