I want auto add query string "_adminToken" for admin URL, example:
http://mydomain.site/wp-admin/plugins.php => http://mydomain.site/wp-admin/plugins.php?_adminToken=123
http://mydomain.site/wp-admin/edit.php?post_type=page => http://mydomain.site/wp-admin/edit.php?post_type=page&_adminToken=123
This is my code:
add_action( 'after_setup_theme', 'my_theme_setup', 999 );
function my_theme_setup() {
add_filter( 'admin_url', 'filter_admin_url' );
}
function filter_admin_url( $url ) {
$query_string = parse_url( $url, PHP_URL_QUERY );
return !$query_string ? $url . '?_adminToken=123' : $url . '&_adminToken=123';
}
But it not working, somebody can help me?
Related
I'm trying to add a column to a custom post type called "Projects" that displays the authors location (city).
I got the column title to show up, but can't seem to render the data in the column itself.
It's displayed elsewhere on the site with:
<?php echo get_the_author_meta( 'city', $author_id ); ?>
SOLVED - HERE IS THE SOLUTION
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
add_filter( 'manage_edit-project_columns', 'test_modify_post_table' );
add_action( 'manage_project_posts_custom_column', 'test_modify_post_table_row', 10, 2 );
}
function test_modify_post_table( $column ) {
$column['city'] = 'Location';
return $column;
}
function test_modify_post_table_row( $column_name, $post_id ) {
$custom_fields = get_userdata($user_id);
switch ($column_name) {
case 'city' :
echo get_the_author_meta( 'city', $author_id );
break;
default:
}
}
I am making my custom permalinks and want to get the post author id in it I have tried many things but all in vain, Please help me on this one.
STEP 1 add this into your functions.php
Guidance here
https://wordpress.stackexchange.com/questions/112719/how-to-use-author-id-in-post-permalink
function wpse_112719_pre_post_link( $permalink, $post, $leavename ) {
if ( strpos( $permalink, '%author%' ) !== false ) {
$authordata = get_userdata( $post->post_author );
$author_id = $authordata->ID;
$permalink = str_replace( '%author%', $author_id, $permalink );
}
return $permalink;
}
add_filter( 'pre_post_link', 'wpse_112719_pre_post_link', 10, 3 );
function wpse_112730_add_rewrite_rules() {
add_rewrite_rule(
"([0-9]+)/(.*)",
'index.php?name=$matches[2]',
'top'
);
}
add_action( 'init', 'wpse_112730_add_rewrite_rules' );
STEP 2 login to admin end and set the permalinks Custom Structure some thing like this : /%postname%/%author%
I have developed plugin and uses shortcode to display.
So, there is two type of items to display like
if get title in url, display single item and if not all items so i have added into if else in condition.
So for that title, i have passed
add_query_arg( array('title' =>sanitize_title($data->get_title())), get_permalink($post_id) );
now url is generated by this is domain.com/post-slug/?title=nameoftitle
but i want to url like domain.com/post-slug/nameoftitle
I have tried following code but not working
function update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
}
add_action( 'init', 'update_rewrite_rules' );
but its not working
I got solution of it. working code is:
function js_update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
$rewrite_rules = get_option( 'rewrite_rules' );
$newVarRegex = '^([^/]*)/title/([^/]*)/?';
// check if the rule exists
if ( !isset( $rewrite_rules[$newVarRegex] ) ) {
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
flush_rewrite_rules();
}
}
function js_on_activation() {
js_update_rewrite_rules();
}
register_activation_hook( __FILE__, 'js_on_activation' );
function js_on_init() {
js_update_rewrite_rules();
}
add_action( 'init', 'js_on_init' );
I have a page template that gets some variables added to the end of the url so I can display data based on what was passed.
ie: mysite.com/search-listins/listing/?address=123+The+Street&mls-number=00000
I need to convert this into a pretty permalink. Somthing like this:
mysite.com/search-listings/listing/123-The-Street OR mysite.com/search-listings/listing/00000-123-The-Street
I tried using this function. But nothing seems to be working. Any thoughts? These are not coming from a custom post type. As you can see, these are not a custom post type. These are MLS items that live outside the wp_ tables.
Function:
function setup_filter_rewrites(){
add_rewrite_rule('search-listings/listing/([^/]*)/?', 'index.php?pagename=search- listings/listing/?address=$matches[1]&mls-number=$matches[2]', 'top');
}
add_action( 'init', 'setup_filter_rewrites' );
function setup_filter_query_vars( $query_vars ){
$query_vars[] = 'listing';
return $query_vars;
}
add_filter( 'query_vars', 'setup_filter_query_vars' );`
I'm always using this code instead of add_rewrite_rule() function:
add_filter( 'query_vars', 'my_query_vars' );
function dlouhavidea_query_vars( $vars ) {
$vars[] = 'address';
$vars[] = 'miles';
return $vars;
}
add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
function ,y_rewrite_rules( $wp_rewrite )
{
$wp_rewrite->rules = array(
'search-listings/listing/address/?([^/]*)/?$' => $wp_rewrite->index . '?pagename=search-listings&address=' . $wp_rewrite->preg_index( 1 ),
'search-listings/listing/miles/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=search-listings&miles=' . $wp_rewrite->preg_index( 1 )
) + $wp_rewrite->rules;
}
You'll than find your vars in get_query_var('address') and get_query_var('miles');
What I'm trying to do is change the link to the main attachment page in Wordpress. Basically, I'm trying to change the word attachment to media.
I'm trying to change:
example.com/parent-category/child-category/post-slug/attachment/attachment-name/
to:
example.com/parent-category/child-category/post-slug/media/attachment-name/
Thanks in advance for any help on this.
I know this is an old question, but I just stumbled on it and thought it was worth a shot;
function __filter_rewrite_rules( $rules )
{
$_rules = array();
foreach ( $rules as $rule => $rewrite )
$_rules[ str_replace( 'attachment/', 'media/', $rule ) ] = $rewrite;
return $_rules;
}
add_filter( 'rewrite_rules_array', '__filter_rewrite_rules' );
function __filter_attachment_link( $link )
{
return preg_replace( '#attachment/(.+)$#', 'media/$1', $link );
}
add_filter( 'attachment_link', '__filter_attachment_link' );