Instantly share code, notes, and snippets.
Last active
October 19, 2023 06:48
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save humayunahmed8/38c29000d4024c1b0189d8e0e2aecb78 to your computer and use it in GitHub Desktop.
Conditional url type selector with codestar metabox framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lhcorp_section_banner_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' != $section_meta['section-type'] ){ | |
return $metaboxes; | |
} | |
$metaboxes[] = array( | |
'id'=>'lhcorp-section-banner', | |
'title'=>__('Banner Settings','lhcorp'), | |
'post_type'=>'section', | |
'context'=>'normal', | |
'priority'=>'default', | |
'sections'=>array( | |
array( | |
'name'=>'lhcorp-section-type-banner', | |
'icon'=>'fa fa-image', | |
'fields'=>array( | |
array( | |
array( | |
'id'=>'button-label', | |
'type'=>'text', | |
'title'=>__('Button Label','lhcorp'), | |
'default'=>__('Get Started','lhcorp') | |
), | |
array( | |
'id' => 'button-url-type', | |
'type' => 'select', | |
'title' => __('Button URL Type', 'lhcorp'), | |
'options' => array( | |
'custom_url' => __('Custom URL', 'lhcorp'), | |
'wp_page' => __('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', '==', 'wp_page'), | |
), | |
array( | |
'id' => 'button-link-custom-url', | |
'type' => 'text', | |
'title' => __('Custom URL', 'lhcorp'), | |
'default'=>__('#','lhcorp'), | |
'dependency' => array('button-url-type', '==', 'custom_url'), | |
), | |
array( | |
'id' => 'button-link-custom-url-target', | |
'type' => 'select', | |
'title' => __('Custom URL Target', 'lhcorp'), | |
'options' => array( | |
'same_tab' => __('Same Tab', 'lhcorp'), | |
'new_tab' => __('New Tab', 'lhcorp'), | |
), | |
'dependency' => array('button-url-type', '==', 'custom_url'), | |
), | |
) | |
) | |
) | |
); | |
return $metaboxes; | |
} | |
add_filter('cs_metabox_options','lhcorp_section_banner_metabox'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Banner Button URL | |
$button_url_type = isset($lhcorp_section_meta['button-url-type']) ? $lhcorp_section_meta['button-url-type'] : 'same_tab'; | |
if ('wp_page' === $button_url_type && !empty($lhcorp_section_meta['button-link-wp-page'])) { | |
$button_url = get_permalink($lhcorp_section_meta['button-link-wp-page']); | |
$link_target = '_self'; | |
} elseif ('custom_url' === $button_url_type && !empty($lhcorp_section_meta['button-link-custom-url'])) { | |
$button_url = esc_url($lhcorp_section_meta['button-link-custom-url']); | |
// $link_target = '_blank'; | |
$link_target = isset($lhcorp_section_meta['button-link-custom-url-target']) && $lhcorp_section_meta['button-link-custom-url-target'] === 'new_tab' ? '_blank' : '_self'; | |
} else { | |
$button_url = '#'; | |
$link_target = '_self'; | |
} | |
$button_label = isset($lhcorp_section_meta['button-label']) ? esc_html($lhcorp_section_meta['button-label']) : ''; | |
?> | |
<a class="btn btn-primary" href="<?php echo esc_url($button_url); ?>" target="<?php echo esc_attr($link_target); ?>"> | |
<?php echo $button_label; ?> | |
</a> |
Author
humayunahmed8
commented
Oct 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment