Last active
January 18, 2016 10:43
-
-
Save herooutoftime/e82f47671e85f9fe8c0b to your computer and use it in GitHub Desktop.
Wordpress Shortcode for links in MODx similar style
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 | |
public function generate_wp_link($atts) | |
{ | |
$atts = shortcode_atts( array( | |
'id' => false, | |
'text' => false, | |
'modal' => false, | |
), $atts, '~' ); | |
list($key, $value) = explode(':', $atts['id']); | |
if(is_numeric($atts['id'])) | |
$link = get_page_link($atts['id']); | |
switch($key) { | |
case 'option': | |
$func = 'ot_get_' . $key; | |
$id = $func($value); | |
$link = get_page_link( $id ); | |
break; | |
case 'post': | |
$link = get_permalink( $value ); | |
break; | |
case 'page': | |
$link = get_page_link( $value ); | |
break; | |
} | |
if(in_array($key, get_taxonomies() )) { | |
$link = get_term_link( (int) $value, $key ); | |
} | |
if($atts['modal'] || !is_null($atts['modal']) || !empty($atts['modal'])) { | |
$data = 'data-toggle="modal" data-target="#'. $atts['modal'] .'"'; | |
$modal =<<<MODAL | |
<!-- Modal --> | |
<div class="modal fade" id="{$atts['modal']}" tabindex="-1" role="dialog" aria-labelledby="{$atts['modal']}Label"> | |
<div class="modal-dialog" role="document"> | |
<div class="modal-content"> | |
</div> | |
</div> | |
</div> | |
MODAL; | |
} | |
if(!$atts['text'] || is_null($atts['text']) || empty($atts['text'])) | |
return $link; | |
return '<a href="' . $link . '" ' . $data . '>' . $atts['text'] . '</a>' . $modal; | |
} | |
add_shortcode( '~', array($this, 'generate_wp_link')); | |
function ajax_change_template($template) { | |
global $wp; | |
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { | |
return locate_template(array('ajax.php')); | |
} | |
else { | |
return $template; | |
} | |
} | |
add_filter('template_include', 'ajax_change_template'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment