Skip Navigation

[Resolved] Obtaining the post edit link for a content template

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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 josephC-5 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1360819

Hi there,

I have a page that contains a view that references a content template. I want to add a link on the public face of the site for logged in users so when they click it they'll go directly to the content template. (Yes, it's unusual, but this is what I want.)

When I use the [wpv-post-edit-link] shortcode, the edit link refers to the page the content template appears on, not the template itself. Is there a way to accomplish this goal? The "item" attribute seems close but it's not quite there.

Thank you!

Saul

#1360947

Hi, are you asking for a way to allow Users to edit a Content Template directly from the front-end of the site using a Form? Or a link to edit the Content Template in wp-admin? Or just to see the Content Template displayed on the front-end of the site?

Forms aren't designed to edit Content Templates, so I don't have a good solution available for that. You might be able to rig something up with a post edit Form and some custom code, but I wouldn't recommend it.

To link to the Content Template in wp-admin, you would create a custom HTML link to the desired wp-admin URL.

To see the Content Template displayed on the front-end of the site, I guess you could insert it in some custom Page using the wpv-post-body shortcode, then create a link to that page.

Did I misunderstand the request?

#1361411

Hi Christian,

Thanks for your response. I'm sorry if my question was vague. I was asking about creating a link to edit a Content Template in the WP dashboard. I was hoping there was a dynamic way to obtain the post ID of the content template so that I wouldn't have to hardcode the custom link (so it would be portable across sites). Do you suppose I could do that through a custom shortcode written in PHP?

Thanks!

Saul

#1361681

Oh okay that makes sense, I just wasn't sure. You could use a custom shortcode to return the post ID of the template with WP_Query. Content Templates are stored in the posts table as post-type "view-template". The slug will be unique among all the other view-template posts. So a custom shortcode would need to accept the Content Template slug as a parameter, then call WP_Query to get a post by slug (name) and post type. That shortcode should return a numeric Content Template ID, or null if it doesn't exist.

Maybe something like this:

// 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');

Then you would build a link tag using that shortcode, like this:

<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>
#1361703

This is exactly what I was trying to accomplish. The code works beautifully.

Thanks, Christian!

Saul