Created
March 30, 2015 18:31
-
-
Save nathansh/44997f311f9614effe93 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Creates an admin edit post link. Can be a custom link to something like an admin page, | |
* ACF options page, or the default post_edit_link | |
* | |
* @param $page type|string/int For a string, a php page like nav-menus.php, or the slug of an options page | |
* which will result in admin.php?page= being put before the slug. | |
* In either case wp-admin/ is prepended. | |
* A post ID can also be supplied for a default edit_post_link | |
* | |
* @param $target type|string The ID to skip to on the edit page, like a field group. | |
* Don't use a '#' in front of the ID. | |
* Note: this can also be a query string argument, but DO include | |
* the '?' first. | |
* | |
* | |
* @param $label type|string The link's text label. Default: 'Edit' | |
* | |
* @return string Html of edit link | |
* @todo Add param for specifying user privleage | |
* | |
*/ | |
function d7_get_edit_link($page = false, $target = false, $label = 'Edit') { | |
$link = false; | |
$output = false; | |
if ( current_user_can('activate_plugins') ) { | |
// If a page hasn't been provided, just do edit_post_link | |
if ( !$page ) { | |
$id = get_the_ID(); | |
// Only do this if the ID isn't 1 | |
if ( $id && $id !== 1 ) { | |
$link = get_edit_post_link($id); | |
} | |
} | |
// If an edit page has been provided | |
if ( $page ) { | |
// If $page is a post ID, do a get_edit_post_link | |
if ( is_int($page) ) { | |
$link = get_edit_post_link($page); | |
} else { | |
// Figure out if a .php link has been given or not | |
$parts = explode('.', $page); | |
// Yes, php | |
if ( count($parts) > 0 && end($parts) == 'php' ) { | |
$link = 'wp-admin/' . $page; | |
} else { | |
// This is an options page | |
$link = 'wp-admin/admin.php?page=' . $page; | |
} | |
// Final URL | |
$link = get_bloginfo('url') . '/' . $link; | |
} | |
} | |
// If a target id (probably an ACF field group) has been provided | |
if ( $target ) { | |
// Only add a # if the target doesn't start with a ? | |
if ( substr($target, 0, 1) != '?' ) { | |
$link .= '#'; | |
} | |
$link .= $target; | |
} | |
if ( $link ) { | |
$output = '<a href="' . $link . '" class="post-edit-link">' . $label . '</a>'; | |
} | |
return $output; | |
} else { | |
return false; | |
} | |
} | |
function d7_edit_link($page = false, $target = false, $label = 'Edit') { | |
$link = d7_get_edit_link($page, $target, $label); | |
if ( $link ) { | |
echo $link; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment