Last active
May 26, 2020 01:53
-
-
Save RadGH/f0f8e798312837b46e0960241a9373cf to your computer and use it in GitHub Desktop.
[ACF] Displays a link to edit a field group in the title of a field group for user and taxonomy pages on the backend.
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 | |
/** | |
* Displays a link to edit a field group in the title of a field group for user and taxonomy pages on the backend. | |
* I recommend moving the JS/CSS to your plugin/theme instead of loading it here. But this is a nice way to keep it portable. | |
* | |
* @param $fields | |
* @param $post_id | |
* | |
* @return mixed | |
*/ | |
function rs_display_edit_field_group_icon_on_user_profile( $fields, $post_id ) { | |
if ( !is_admin() ) return $fields; | |
// Get the screen being viewed | |
$screen = function_exists('get_current_screen') ? get_current_screen() : false; | |
if ( !$screen ) return $fields; | |
// Ignore post types because they have a button already | |
if ( $screen->post_type && !$screen->taxonomy ) return $fields; | |
// Create a handle which will appear after the field group title (h2 tag) | |
echo '<div class="rs-field-group-handle" data-post-id="'. esc_attr( $fields[0]['parent'] ) .'"></div>'; | |
// ONE TIME ONLY - Print the script on the page that will convert handles into buttons. | |
static $script_displayed = false; | |
if ( !$script_displayed ) { | |
$script_displayed = true; | |
?> | |
<script type="text/javascript" id="rs-field-group-handler-script"> | |
jQuery(function() { | |
var $handles = jQuery('.rs-field-group-handle'); | |
$handles.each(function() { | |
var group_id = jQuery(this).attr('data-post-id'); | |
var $target = jQuery(this).prev('h2'); | |
if ( $target.length < 1 ) return; | |
$target.each(function() { | |
var $a = jQuery('<a>'); | |
$a | |
.addClass('rs-field-group-handle-link') | |
.attr('href', 'post.php?post='+ group_id +'&action=edit' ) | |
.attr('title', 'Edit field group') | |
.attr('target', '_blank') | |
.html('<span class="dashicons dashicons-admin-generic"></span>'); | |
jQuery(this).append($a); | |
}); | |
}).remove(); | |
jQuery('#rs-field-group-handler-script').remove(); | |
}); | |
</script> | |
<style type="text/css"> | |
a.rs-field-group-handle-link { | |
font-size: 14px; | |
margin-left: 4px; | |
text-decoration: none; | |
color: #494c4e | |
} | |
a.rs-field-group-handle-link .dashicons { | |
font-size: 16px; | |
position: relative; | |
top: 2px | |
} | |
</style> | |
<?php | |
} | |
return $fields; | |
} | |
add_filter( 'acf/pre_render_fields', 'rs_display_edit_field_group_icon_on_user_profile', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment