i have to add prefix in some pages of my website and it show the conent of my existing page (mean without prefix page) in wordpress - wordpress

$items = array( 'page' );
foreach( $items as $item )
{
add_filter( $item . '_link', 'my_link', 99, 2 );
}
function my_link($permalink, $post )
{
$pagearr = array('daily-dairy', 'free-downloads','latest-essay-topics','practice- tests','practice-exercises','video-lectures','contacts','sample1','sample3');
$arr = explode('/',$permalink);
if(in_array($arr[sizeof($arr)-2],$pagearr)){
$permalink = str_replace( get_site_url(), get_site_url() . '/institute', $permalink );
return $permalink;
}else{
return $permalink;
}
}
This code adds a prefix in my page URL but it shows page not found when add the prefix. I have to show the content without the prefix page.

you haven't mentioned here that which hook you are using for these functions, you have written in your code. Look at the code below and tell me if you find it working on your end.
function change_blog_links($post_link, $id=0){
$post = get_post($id);
if( is_object($post) && $post->post_type == 'post' || $post->post_type == 'page'){
return home_url('/YOUR_PREFIX/'. $post->post_name.'/');
}
return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);

Related

Display multiple ads Between post

For multiple ads on post
I have implemented first code and it perfectly work. But iam not good at coding so can you tell me how to show multiple ads? This second code how to implement it properly can you help me with it?
Also for multiple ads do i need to create second in article ads code? Or i can use the same ads code twice?
//Insert ads after third paragraph of post content.
add_filter( 'the_content', 'insert_post_ads' );
function insert_post_ads( $content ) {
$ad_code = '<div>Insert your Ads code here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 3, $content );
}
return $content;
}
// Parent Function
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content )
{
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
And this second code:
$first_ad_code = ‘Ads code goes here’;
$second_ad_code = $first_ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $first_ad_code, 3, $content );
$content = prefix_insert_after_paragraph( $second_ad_code, 6, $content );
return $content;
}
return $content;
}
I have collected this two code from this blog: https://www.codefixup.com/wordpress-insert-ads-between-posts/
i have astra child theme. these code will be pasted on child theme function.php
Please just edit these code as a one pls.

Get Post ID of post you're currently editing in WordPress

I'm attempting to get the ID of the post I am editing in functions.php for the purpose of dynamically rewriting a custom post type slug.
This is what I'm working with so far.
function change_post_type_slug( $args, $post_type ) {
if ( 'custom_post' == $post_type ) {
global $post;
$location = get_field('custom_field', $post->ID);
$args['rewrite']['slug'] = $location;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
I am not sure if the hook register_post_type_args is firing before I am able to get the ID, or if this is even the best way to go about what I am trying to accomplish. Can't find much out there on the subject.
I was able to get it to work with the following:
function change_post_type_slug( $args, $post_type ) {
if ( 'lead_page' == $post_type ) {
$post_id = $_GET['post'];
$location = get_field('leadpage_location', $post_id);
$args['rewrite']['slug'] = $location->post_name;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
However it resulted in a notice on the front-end:
Notice: Undefined index: post in /path/to/wordpress/functions.php on line 623
Line 623 is $post_id = $_GET['post'];
You should use the updated_postmeta hook for this, as its run every time you update your custom fields.
Then you can update your post data with wp_update_post() function.
add_action( 'updated_postmeta', function( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( 'location' === $meta_key ) {
wp_update_post([
'ID' => $object_id,
'post_name' => $meta_value,
]);
}
}, 10, 4 );
Update:
Try this:
function change_post_types_slug( $args, $post_type ) {
if ( 'your-custom_post' === $post_type ) {
// Check and get the custom post ID
$id = isset($_GET[ 'post' ]) ? $_GET[ 'post' ] : '' ;
// $location = get_field('leadpage_location', $id);
$args['rewrite']['slug'] = 'new-slug-here';
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );
Try this out:
function change_post_type_slug( $args, $post_type ) {
if ( 'lead_page' === $post_type && is_admin() && $_GET['action'] === 'edit' ) {
$post_id = $_GET['post'];
$location = get_field('leadpage_location', $post_id);
$args['rewrite']['slug'] = $location->post_name;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
It adds two more conditionals, to check if you're on the admin screen and to check of there is a GET parameter of edit. Probably overkill to do is_admin() as well, but now you're super safe.

How to rewrite URL of custom post type without post type in URL

I am working on a tour site. Where they have a custom post type built with a custom plugin.
That custom post type has base slug like "/destination/".$post->post_name.
They want to me to remove that base slug so that it could be only $post->post_name
I have tried a code from internet listed below.
It works for single level for that destination.
But when I have a parent destination like New York in United State America.
So it does not work.
Here is an example:
function update_destination_page_link_filter($post_link, $post, $leavename = null, $sample = null ) {
if ( $post->post_type === 'destination' ) {
$post_link = str_replace('/destination/', '/', $post_link);
if($post->post_parent !== 0){
$parent_slug = get_parent_link($post->post_parent, '');
$post_link = '/'.$parent_slug.$post->post_name;
}
$post_link = update_url_base( $post, $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'update_destination_page_link_filter', 1, 3 );
function allow_destination_direct_by_name( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'destination', 'page' ) );
}
add_action( 'pre_get_posts', 'allow_destination_direct_by_name', 1);
Single Level
http://siteurl/united-state-america works well
http://siteurl/united-state-america/new-york not working. It should open the new-york location page but it is showing 404
It may also be more specification in location
Like http://siteurl/united-state-america/new-york/brooklyn
The following code may help you, in this regards.
add_filter( 'post_type_link', 'my_post_type_link', 10, 3);
function my_post_type_link($permalink, $post, $leavename) {
if ($post->post_type == <your-post-type>)
{
$p_name=$post->post_name;
$parent_slug = get_parent_link($post->post_parent, '');
if (isset($parent_slug) && !empty($parent_slug))
{
$permalink = home_url( "" . $parent_slug . "/" . $p_name . "/");
}
}
return $permalink;
}
add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array');
function my_rewrite_rules_array($rules) {
$rules = array('([^/]*)/([^/]*)/?$' => 'index.php?post_type=<your-post-type>&name=$matches[2]&meta=$matches[1]') + $rules;
return $rules;
}

Wordpress: remove one category in permaling /%category%/%postname%/

I am looking for a solution in order to remove /%category%/ from my permalinks for only one category ("general").
My permalink is currently set to /%category%/%postname%/. How can I create the following URLs only for the "general" category /%postname%/ ?
Thanks!
You can set custom structure in Permalinks Settings: Dashboard - Settings>Permalinks
This works for me:
function remove_uncategorized( $permalink, $post, $leavename ) {
if( $post->post_type != 'post' ) return $permalink;
$cats = get_the_category($post->ID);
if( ! count($cats) ) return $permalink;
usort($cats, '_usort_terms_by_ID');
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
$category_object = get_term( $category_object, 'category' );
return _clear_uncategorized($category_object, $permalink);
}
function _clear_uncategorized($cat, $permalink) {
if( $cat->slug == 'sin-categoria' ) {
return str_replace('%category%/', '', $permalink);
}
$parent = $cat->parent;
if ( !$parent )
return $permalink;
return _clear_uncategorized($parent, $permalink);
}
add_filter( 'pre_post_link', 'remove_uncategorized', 9, 3 );

How can I use category IDs in WordPress permalinks?

I want to use something like:
http://example.com/%category_id%/%postname%/
for the permalink structure.
For example, if a post has a category with an ID of 3, then the URL for the post will be
http://example.com/3/post-name/
Does anyone know how this can be done? I don't mind modifying WordPress core.
This code adds the %category_id% rewrite tag, and filters post permalinks to replace them with the actual category ID (lowest if there are multiple categories). You can place this in a plugin or in your theme file.
add_action( 'init', 'so6159452_init' );
function so6159452_init()
{
add_rewrite_tag( '%category_id%', '([0-9]+)' );
}
add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
function so6159452_post_link( $permalink, $post )
{
if ( false !== strpos( $permalink, '%category_id%' ) ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category_id = $cats[0]->cat_ID;
} else {
// Error: no category assigned to this post
// Just use a dummy variable
$category_id = '0';
}
$permalink = str_replace( '%category_id%', $category_id, $permalink );
}
return $permalink;
}

Resources