0

i have external link stored in a variable $extlink which is generated based on different selections in the post.

how do i set the permalink to be this $extlink so that when i click on the post it takes me to the link via program/code

    <?php while (have_posts()) : the_post(); ?>             

                    <div class="bookindex">

                     <?php the_title();?>

                     <?php 
                        $ext1 = get_field('bookzone');
                        $extlink="http://".$ext1.".com";
                        add_filter( 'post_link', 'external_permalink', 10, 2 ); 
                        function external_permalink( $link, $post )
                        {

                             $meta = get_post_meta( $post->ID, $extlink , TRUE );
                             $url  = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );

                             return $url ? $url : $link;
                        }
                    ?>      

                </div>
<?php endwhile; ?>
2
  • Don't put your add_filter() and its associated callback function in The Loop. They should be separate. Also, do you want to filter every URL, or only specific ones?
    – Pat J
    Commented Jul 4, 2014 at 19:43
  • i want it for all...each and every post created Commented Jul 5, 2014 at 8:05

1 Answer 1

0

I would think something is wrong here:

$meta = get_post_meta( $post->ID, $extlink , TRUE );

This is the function:

get_post_meta( int $post_id, string $key = '', bool $single = false )

You are setting the $extlink as the $key parameter, but it is really a meta key? From your explanation, it seems to be a value of the bookzone key ...

If the above makes sense, then inside your function, fix the following:

$meta = get_post_meta( $post->ID, 'bookzone', TRUE );

As Pat J said, also remove the add_filter and the function itself outside the while loop. When you set this particular filter once, Wordpress will execute your filter's function whenever the permalink is called inside the Wordpress loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.