0

Is this possible? If so, how?

I am linking to a non-existing post/page where I get a 404 error. I want to have it to get the url of the non-existing post and make it a link on my 404 page, but I don't want it to throw an error (so no 404 page, just a simple page). When I click this, I want it to go to the page where I can add posts (from the front end with a plugin called User Submitted Posts). Is this possible?

BTW: showing related posts by getting the link would be nice too. But I don't think that's possible.

1 Answer 1

1

If in your template you create a 404.php all not-found requests will be handled by this template file, and there you can put whatever you want.

Just as example, in the 404.php file is possible to use something like (it's just a draft):

<?php get_header(); ?>
<h3>
<?php
$slug = get_query_var('name');
if ( $slug ) {
    $name = ucwords(str_replace('-', ' ', $slug));
    printf( __('Searching for post &quot;%s&quot;?') . ' ', $name );
    _e('It does not exists, maybe you can create it.');
} else {
    $name = '';
    _e('Content you required not exists, maybe you can create it.');
}
?>
<h3>
<div id="user-submitted-posts-wrapper" data-name="<?php echo $name; ?>">
<?php echo do_shortcode('[user-submitted-posts]'); ?>
</div>

<script>
jQuery().ready(function($) {
   var name = $('#user-submitted-posts-wrapper').data('name');
   if ( name != '') $('.usp-title input').eq(0).value(name);
});
</script>

<?php get_footer(); ?>

The plugin do not support precompiling post title, but you can fill it with jquery. I inserted the script echoing it directly in the footer for answer semplicity, but is much better register and enqueue it using wp_register_script and wp_equeue_script. You have also ensure that jquery is included in the page, and if you use wp_register_script you can put jquery in the script dependencies.

Advanced Task

If you want to differentiate some 404 from others you can hook the template_include filter. E.g. if you want to show a different 404 if the required post type is 'product', you can use:

add_filter('template_include', 'change_404_template');

function change_404_template($template) {
  if ( is_404() && ( get_query_var('post_type') == 'product' ) ) {
      return get_stylesheet_directory() . '/my-custom-template-for-not-found.php' ;
  }
  return $template;
}

Different approach is make a full redirect on 404. This is less performant, because 2 requests are sended to server (one that cause 404 and the second is the redirect) , but in some cases this method can be useful. If you want to follow this way hooks on template_redirect action:

add_action('template_redirect', 'redirect_404');

function redirect_404() {
  if ( is_404() && ( get_query_var('post_type') == 'product') ) ) {
    wp_redirect( home_url() ); // I redirect to home, you can redirect wherever you want
    exit();
  }
}
9
  • Thanks. G.M. , but I need something more generic. If the post does not exist, it must show me no error actually (so no 404), but a page that has a link to 'make this post' and getting the post name.... Commented Aug 4, 2013 at 7:17
  • And what's stopping you to insert <?php do_shortcode('[user-submitted-posts]'); ?> in the 404.php file?
    – gmazzap
    Commented Aug 4, 2013 at 7:24
  • Hmm, interesting one... So writing in a 404 page... Did not think about that... The problem might be that people still have to write the postname that they did not found... Is there a way to catch the postname and put that in an input field in this one? If there is, than this problem is solved. Commented Aug 4, 2013 at 7:50
  • In WP url there isn't post title, but slug. WP obtain slug from title using func sanitize_title that is not full reversible. In my code, if you see, there is $slug = get_query_var('name') that try to cacht the requested post name and convert it to a more readable title: $name = ucwords(str_replace('-',' ',$slug)) so if someone click the link http://yousite.com/2013/07/my-test-post/ ('my-test-post' is the slug) and this post not exists 404.php is called and $name var will be "My Post Slug" and you can use it as you want.
    – gmazzap
    Commented Aug 4, 2013 at 8:41
  • Thanks for the info, that's clear. Anyhow, I tried your first code and set it on 404.php, but I got an error unexpected ',' Commented Aug 4, 2013 at 8:43

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.