custom pages give 404 error title in Wordpress - wordpress

I'm running a site powered by WordPress with extra pages...
To integrate these pages with the WordPress theme I use this code:
<?php
$blog_longd='Title'; // page title
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
get_header();
?>
html code
<?php
get_sidebar();
get_footer();
?>
This works fine, however page title shows always 404 Error Page (not "Title").
It seems that $wp-query->is_404 is always set to true. I tried overriding this value but it doesn't seem to work. I tried fixing it by putting header status 200 above function get_header()..also it doesn't work.
Any suggestions?
Thanks

I know it has been a long time since you asked but I had the problem and here is the solution.
<?php
require('./wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
$wp->send_headers();
get_header();
echo "HELLO WORLD";
get_footer();
?>

Maybe clumsy, but if you implement the wp_title filter, you can change the title to what you want. You can add this code to the header of each custom page:
add_filter('wp_title', 'replace_title');
function replace_title() {
return 'My new title';
}
If you want it a bit cleaner, use a smarter version of this filter to a plugin, and set only the global variable (here $override_title) in your page:
add_filter('wp_title', 'replace_title_if_global');
function replace_title_if_global($title) {
global $override_title;
if ($override_title) {
return $override_title;
}
return $title;
}

There is code in the file class-wp.php:
function handle_404() {
...
// Don't 404 for these queries if they matched an object.
if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
status_header( 200 );
return;
}
...
}
that handles 404 status for various of pages.
The stack of functions of this code is:
1) wp-blog-header.php:14, require()
2) function.php:775, wp()
3) class-wp.php:525, WP->main()
4) class-wp.php:491, handle_404()
So you have two ways to handle the situation:
1)
require('wp-blog-header.php');
function status_header( 200 );
2) more correct would be insert your own function here
if ( your_own_function() || ((is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object()) ) {
that returns true when your custom page is requested

Related

Wordpress. How to use shortcode with if else statement?

I have following code but it's displaying shortcode in text rather than showing results of shortcode.
$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
// we know now the user is logged-in, so we can use his/her ID to get the user meta
$um_value = get_user_meta( $curr_user_id, 'user_phone', true );
// now we check if the value is correct
if ( ! empty( $um_value ) && $um_value == 'French' ) {
// if so we can output something
echo '[ld_course_list course_category_name="french" col=4 progress_bar=true]';
} else {
// else the code, eg.
echo '[ld_course_list course_category_name="english" col=4 progress_bar=true]';
}
}
Above code will result in text below rather than showing results of the shortcode
[ld_course_list course_category_name="english" col=4
progress_bar=true]
How do I change my code so it actually runs shortcode?
Thank you.
Following code worked. Only problem is do_shortcode is breaking stylesheet. Anyone know why or how to fix it?
<?
$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
// we know now the user is logged-in, so we can use his/her ID to get the user meta
$um_value = get_user_meta( $curr_user_id, 'user_phone', true );
// now we check if the value is correct
if ( ! empty( $um_value ) && $um_value == 'French' ) {
// if so we can output something
echo do_shortcode('[ld_course_list course_category_name="french" col=4 progress_bar=true]');
} else {
// else the code, eg.
echo do_shortcode('[ld_course_list course_category_name="english" col="4" progress_bar="true"]');
}
}
?>
You are using it in wrong way, the method you are trying is used inside wordpress pages, posts etc.
To use inside plugin, theme or any other .php file, use WordPress function called do_shortcode() that lets you add shortcodes directly in your themes and plugins.
Add it like this:
echo do_shortcode("[ld_course_list course_category_name="english" col=4 progress_bar=true]");

Wordpress if post or blog category statement

got a little problem here on Wordpress.
I am trying to use PHP to check whether the page I am on is a category page or a post page if it isn't on either it will do something else nothing.
The code I have is:
<?php if( (!is_category($category)) || (!is_single($post)) ) { ?>
Do something...
<?php } ?>
When I try it without the or the category bit works fine. When I join them together the code stops working.
I think what you're actually trying to test is if it's not a category and not a post. So change your condition accordingly:
if( (!is_category($category)) && (!is_single($post)) ) {

Wordpress Redirects to Parent Pages or Categories

What I am trying to do is to redirect 404 to the next possible page/category. The following is a detailed description of what I want:
If the user falls on the following:
www.domain.com/about/(any text that generates 404) it should automatically redirect to www.domain.com/about
www.domain.com/category/(any text that generates 404) it should automatically redirect to www.domain.com/category
www/domain.com/about/sub-about/(any text that generates 404) it should automatically redirect to www.domain.com/about/sub-about
I hope that describes what I am looking for. I would love if my fellows here post a solution to this. I have tried using the 404 redirected plugin but it is not auto redirecting. I am open to any solution as long as it works on wordpress.
Inserting a code like this one in the 404 script might work:
<?php
$Referrer = wp_get_referer( ) ;
if ( $Referrer = 'http://www.domain.com/' ) {
echo '<script type="text/javascript">window.location ="http://www.domain.com/about/";</script>' ;
}
elseif ( $Referrer = 'http://www.domain.com/about/' ) {
echo '<script type="text/javascript">window.location ="http://www.domain.com/about www.domain.com/category/";</script>' ;
}
elseif ( $Referrer = 'http://www.domain.com/about www.domain.com/category/' ) {
echo '<script type="text/javascript">window.location ="http://www.domain.com/category www/domain.com/about/sub-about/";</script>' ;
}
else {
echo "404 Message" ;
}
?>

trouble adding jQuery UI to Wordpress admin page

I'm attempting to add a datepicker to a custom meta box in wordpress. If I enqueue the script like so
// add stylesheets for date picker
add_action('admin_print_styles', 'add_datepicker_styles');
function add_datepicker_styles() {
global $post;
$styleFile = get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
if(file_exists($styleFile) && $post->post_type == 'show') {
wp_enqueue_style("datepicker-css", $styleFile);
}
}
// add javascript for date picker
add_action('admin_print_scripts', 'add_datepicker_js');
function add_datepicker_js() {
global $post;
$jsFile = get_bloginfo("template_url").'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js';
if(file_exists($jsFile) && $post->post_type == 'show') {
wp_enqueue_script("datepicker-js", $jsFile);
}
}
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
global $post;
if($post->post_type == 'show') {
echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
}
}
I get an error that jQuery('#show_date').datepicker(); is not a function. When I check the processed source code I see that jQuery is loaded by default but the style and jQuery UI script are not loaded at all. The code attached to the admin_head hook loads fine and if I echo the script and styles in this function it works the way I want it to.
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
global $post;
if($post->post_type == 'show') {
echo "<link rel='stylesheet' type='text/css' href='".get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css'."' />";
echo "<script type='text/javascript' src='".get_bloginfo('template_url').'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js'."'></script>";
echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
}
}
So is it a problem with the wp_enqueue_style/script() functions or with the add_action hooks? I'm new to wordpress so I'm not really sure how to troubleshoot this. I've done a google search and everywhere I go the code looks like mine. Also if you are wondering the paths to the files are correct.
Can anyone think of a solution to my problem? Is it wrong to just echo the script and styles or should I enqueue them?
Thanks!
EDIT:
file_exists($jsFile) returns false. if I remove the conditional check for the file the code works.. But why? how else can you check for a file that exists using a url beginning with http:// as opposed to a local file path?
If you want to get a local path for your theme, you can use get_theme_root():
$styleFile = get_theme_root().'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
if(file_exists($styleFile) && $post->post_type == 'show') {
From php.net/file_exists:
this code here is in case you want to check if a file exists in
another server:
<?php function fileExists($path){
return (#fopen($path,"r")==true); } ?>
unfortunately the file_exists can't reach remote servers, so I used
the fopen function.

How to add ads to the end of RSS on wordpress?

I have added a function to functions.php on my theme.
function insertAds($content) {
$content = $content.' add goes here';
return $content;}
add_filter('the_content_feed', 'insertAds');
add_filter('the_excerpt_rss', 'insertAds');
The problem is that I'm having the add displayed under each content, and not at the end of the rss page. How can I fix that?
WordPress doesn’t offer a hook for what you want to do. In which element would you place the ad?
The usual RSS-2-Feed has meta data and items (the content). There is no other element.
See wp-includes/feed-rss2.php for details.
Update
Adjust the following code to your needs and put the file into your plugin directory:
<?php
/*
Plugin Name: Last man adding
Description: Adds content to the last entry of your feed.
Version: 0.1
Author: Thomas Scholz
Author URI: http://toscho.de
Created: 31.03.2010
*/
if ( ! function_exists('ad_feed_content') )
{
function ad_feed_content($content)
{
static $counter = 1;
// We only need to check this once.
static $max = FALSE;
if ( ! $max )
{
$max = get_option('posts_per_rss');
}
if ( $counter < $max )
{
$counter++;
return $content;
}
return $content . <<<MY_ADDITIONAL_CONTENT
<hr />
<p>Place your ad here. Make sure, your feed is still
validating</p>
MY_ADDITIONAL_CONTENT;
}
}
add_filter('the_content_feed', 'ad_feed_content');
add_filter('the_excerpt_rss', 'ad_feed_content');
Is this the effect you had in mind? As you can see, adding content is rather easy. :)

Resources