Problem: On the front-end of my site, I would like to be able to display a link to edit a Content Template in wp-admin. I would like to be able to use this solution across multiple sites, so it should be dynamic. Since I need to know the template ID to create the wp-admin link, I need a way to get the template ID given the template slug.
Solution: It will require a custom shortcode. That shortcode should accept the slug of the Content Template you want to edit, and should return a numeric Content Template ID if it exists, or return null if no matching template is found. Use that shortcode to construct a link to the editor in wp-admin. Shortcode:
// get a content template id, given the content template slug function tssupp_get_ct_id($atts, $content='') { $atts = shortcode_atts( array( 'slug' => '' ), $atts ); $args = array( 'post_type' => 'view-template', 'post_status' => 'publish', 'name' => $atts['slug'] ); $cts = new WP_Query( $args ); $id = isset($cts->posts[0]->ID) ? $cts->posts[0]->ID : null; return $id; } add_shortcode( 'tssupp-get-ct-id', 'tssupp_get_ct_id');
In the site contents:
<a href="/wp-admin/admin.php?page=ct-editor&ct_id=[tssupp-get-ct-id slug='books'][/tssupp-get-ct-id]&action=edit">Edit the Books Content Template</a>
Change the slug books to match the slug of the desired Content Template. Change Edit the Books Content Template to display the desired link text.
Relevant Documentation:
https://codex.wordpress.org/Shortcode_API
https://developer.wordpress.org/reference/classes/wp_query/
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 4 replies, has 2 voices.
Last updated by 5 years, 2 months ago.
Assisted by: Christian Cox.