WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 years ago

#10544 closed enhancement (duplicate)

Specify tags on new posts through the URL

Reported by: rvenable Owned by:
Milestone: Priority: low
Severity: minor Version: 2.8.1
Component: Plugins Keywords:
Focuses: Cc:

Description

I want to create links like such: "Add a new post with tag X" with a URL like this: wp-admin/post-new.php?new_tag=X

Clicking on the link would take you to the Add New Post admin panel with tag X already added.

Unfortunately, there is no clean way for a plugin to make this work. The only clean way would be to add a filter on 'terms_to_edit' which adds the new_tag var to the end of the terms.

Unfortunately, get_terms_to_edit() returns early if there is no post ID and never reaches the necessary filter:

function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
	$post_id = (int) $post_id;
	if ( !$post_id )
		return false; // <------ BAILS OUT EARLY HERE

	$tags = wp_get_post_terms($post_id, $taxonomy, array());

	if ( !$tags )
		return false;

	if ( is_wp_error($tags) )
		return $tags;

	foreach ( $tags as $tag )
		$tag_names[] = $tag->name;
	$tags_to_edit = join( ',', $tag_names );
	$tags_to_edit = esc_attr( $tags_to_edit );
	$tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );   // <-------- THE FILTER I NEED TO HOOK ONTO TO ADD MY NEW TAG

	return $tags_to_edit;
}

So, a possible solution would be to still apply the filter when bailing out on an empty post_id.

Change History (8)

comment:1 @scribu6 years ago

  • Keywords 2nd-opinion added

comment:2 follow-up: @Denis-de-Bernardy5 years ago

  • Milestone Unassigned deleted
  • Resolution set to duplicate
  • Status changed from new to closed

there's a ticket in trac somewhere that suggests fetching the $_GET variable and doing the same for all fields automatically.

it's also doable using a plugin, too.

comment:3 in reply to: ↑ 2 @rvenable5 years ago

Replying to Denis-de-Bernardy:

there's a ticket in trac somewhere that suggests fetching the $_GET variable and doing the same for all fields automatically.

it's also doable using a plugin, too.

What ticket are you refering to?

comment:5 in reply to: ↑ 4 ; follow-up: @rvenable5 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

Replying to Denis-de-Bernardy:

#8223

for an example of it being done in a plugin:

http://plugins.trac.wordpress.org/browser/sem-admin-menu/trunk/

This ticket is not for adding additional functionality to wordpress (as ticket #8223), but for simply allowing the functionality to be added by a plugin. Right now plugins cannot solve this.

The plugin you mention, from what I can see from scanning the code, succeeds in adding a parent ID to a post via a $_GET param. It does this by simply editing the value of $post->post_parent, which works because the value of $post->post_parent is output as part of the edit menu.

It is not so easy to add a tag to a new post via a $_GET param, and you cannot simply modify some values in the $post variable because they won't be displayed. The function post_tags_meta_box() displays all the tags for a given post. It displays any tags returned by the get_terms_to_edit() function.

So, the only way to get tags to appear on the new post edit page is to go through the get_terms_to_edit() function. Unfortunately, that function bails out early when there is no post ID, making it impossible for a plugin to specify tags to be displayed for a new post. So in essence, the problem is that the function assumes that new posts have no tags, which is what I want to be fixed.

Here is the function: (notice I marked where it bails out)

function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
	$post_id = (int) $post_id;
	if ( !$post_id )
		return false; // <------ BAILS OUT EARLY HERE

	$tags = wp_get_post_terms($post_id, $taxonomy, array());

	if ( !$tags )
		return false;

	if ( is_wp_error($tags) )
		return $tags;

	foreach ( $tags as $tag )
		$tag_names[] = $tag->name;
	$tags_to_edit = join( ',', $tag_names );
	$tags_to_edit = esc_attr( $tags_to_edit );
	$tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy ); // <-------- THE FILTER I NEED TO HOOK ONTO TO ADD MY NEW TAG

	return $tags_to_edit;
}

A fix would either remove the early bail out or add a filter that I can hook onto when it bails out:

function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
	$post_id = (int) $post_id;
	if ( !$post_id )
		return apply_filters('get_terms_to_edit_new_post', false, $taxonomy);
...
}

Pretty simple fix, I think.

comment:6 @scribu5 years ago

  • Milestone set to 3.0

comment:7 @nacin5 years ago

  • Milestone changed from 3.0 to Future Release

comment:8 in reply to: ↑ 5 @johnbillion5 years ago

  • Keywords 2nd-opinion removed
  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from reopened to closed

Closing as dupe of #8223 because we might as well concentrate on all the fields rather than individual ones.

Note: See TracTickets for help on using tickets.