Forum Replies Created

Viewing 15 replies - 256 through 270 (of 520 total)
  • Ah, just reread your initial message. From the error message, it looks like the theme itself is causing the issue:

    Fatal error: Cannot redeclare add_unlink_script() (previously declared in /home/content/06/9931306/html/wp-content/themes/Metrolium/functions.php:554) in /home/content/06/9931306/html/wp-content/themes/Metrolium/backend/functions.php on line 55

    so

    add_unlink_script() appears to be declared both in

    wp-content/themes/Metrolium/functions.php (line 554)

    and in

    wp-content/themes/Metrolium/backend/functions.php (line 55)

    Does the site that does work with this theme have the same version of the theme and can you check if it has the two function.php files…and if they both contain an add_unlink_script function?

    Beyond that, you’ll have to check with the theme author.

    As a quick test, disable all your plugins on the install throwing the error.

    Are you using the exact same plugins on the other install? It only happens if there’s a conflict and it’s most likely a conflict between the theme and a plugin.

    Forum: Fixing WordPress
    In reply to: Fatal Error??

    Is this a new install? It’s possible something may have gotten corrupted or not installed. In particular, see if you have:

    wp-includes/cache.php

    in your install. That’s where the wp_cache_init function is defined.

    Themes and plugins (and WordPress itself) contain functions, and unless they are name spaced (usually by using Object Oriented programming style), then if any two functions from separate sources have the same name there will be a “name collision” and you’ll see that error. Generally which ever function is found first “wins” the other(s) will throw an error saying the function name cannot be redeclared. add_unlink_script is a pretty generic name. Plugin and theme authors will usually add a prefix related to their plugin or theme to make name collision less likely to happen. The author needs to update the name of that function, say to something like:

    metrolium_add_unlink_script

    Your answer was right there in the Codex: Function Reference/get the category page under the “Show All Categories as Links” section 🙂

    Slightly modified/simplified:

    <?php
    $categories = get_the_category();
    if( $categories ) {
        foreach( $categories as $category ) {
            echo "$category->name<br />";
        }
    }
    ?>

    As a general rule, non-WordPress.org themes aren’t supported in these forums.

    But as a theme-agnostic solution, you might try this:

    <p><a href="<?php echo get_permalink( get_the_ID() ); ?>">Read more...</a></p>

    Try this in your theme’s functions.php:

    function filter_handler( $data , $postarr ) {
        if ( !current_user_can( 'manage_options' ) ) {
            $data['post_content'] = preg_replace( "/\[([^\[\]]++|(?R))*+\]/", "", $data['post_content'] );
        }
        return $data;
    }
    add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

    This will prevent anyone other than admin/superadmin from inserting shortcodes (well, it will delete a pair of square brackets and anything in between when a non-admin inserts or updates a page/post – if there’s a better way I haven’t found it yet).

    Check your wp-config.php file and make sure everything with regards to the database is correct. (database name, host, username, password) If any of those items are wrong, then you will get the message:

    Error establishing a database connection

    You would also get that error if MySQL itself wasn’t running.

    I think you should be able to add this to your functions.php in your theme to disable cache:

    define(‘MAGPIE_CACHE_ON’, 0);

    WordPress caches feeds for one hour, so that may be the issue with that.

    If you go to the single post page, you should get a 404. If you go to the category page for the excluded category, then you’ll get a page with not posts. Same with search. Feeds should also exclude those posts in those categories (with the caveat that feeds cache).

    What I do is this. I comment out this line:

    //add_action( 'pre_get_posts', 'exclude_post_by_category' );

    and then go to a category that I want to be excluded (but since the code is commented out, the post(s) for that category should be listed).

    So, in my case, I go here (I’m using Posts Names for permalins):

    http://localhost/category/uncategorized/

    and I see my posts.

    I click on any individual post on the category page, for example

    http://localhost/test-post-2/

    and I see the post.

    Now I uncomment that line in functions.php:

    add_action( 'pre_get_posts', 'exclude_post_by_category' );

    and the go back to the category page and refresh, and voila, no posts show (I get: “Nothing Found – It seems we can’t find what you’re looking for. Perhaps searching can help.”)

    I then go forward in browser history to the single post page (http://localhost/test-post-2/) and it get a 404.

    I just double checked trying both Default and Post Name permalink settings and in both cases it removed the posts (in the exclusion array) from the feed, the category page (and since it’s by category, the page simply had no posts), the blog posts page, and of course the individual posts.

    I used Twenty Thirteen theme, no plugins, and the last “full” code I posted. The code I posted before that would just hide the post itself.

    This is the code you should use, and no additional plugins to hide category.

    function exclude_post_by_category( $query ) {
        $excluded_category_ids = array(1);
        if ( $query->is_main_query() ) {
            if ( ( $query->is_home() || $query->is_category() || $query->is_archive() || $query->is_feed() ) || ( !is_admin() && $query->is_search() ) ) {
                $query->set('category__not_in', $excluded_category_ids);
            } else if ( $query->is_single() ) {
                if ( ( $query->query_vars['p'] ) ) {
                    $page= $query->query_vars['p'];
                } else if ( isset( $query->query_vars['name'] ) ) {
                    $page_slug = $query->query_vars['name'];
                    $post_type = 'post';
                    global $wpdb;
                    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
                }
                if ( $page ) {
                    $post_categories = wp_get_post_categories( $page );
                    foreach ($excluded_category_ids as $category_id ) {
                        if ( in_array( $category_id, $post_categories ) ) {
                            $query->set( 'p', -$category_id );
                            break;
                        }
                    }
                }
            }
        }
    }
    add_action( 'pre_get_posts', 'exclude_post_by_category' );

    Are you using the last code I posted? (the “I believe should be the full code” code) That should be used without any other plugins. Now that it’s morning, I’ll give it one more check with all permalink types.

    And I believe this should be the code in full:

    function exclude_post_by_category( $query ) {
        $excluded_category_ids = array(1);
        if ( $query->is_main_query() ) {
            if ( ( $query->is_home() || $query->is_category() || $query->is_archive() || $query->is_feed() ) || ( !is_admin() && $query->is_search() ) ) {
                $query->set('category__not_in', $excluded_category_ids);
            } else if ( $query->is_single() ) {
                if ( ( $query->query_vars['p'] ) ) {
                    $page= $query->query_vars['p'];
                } else if ( isset( $query->query_vars['name'] ) ) {
                    $page_slug = $query->query_vars['name'];
                    $post_type = 'post';
                    global $wpdb;
                    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
                }
                if ( $page ) {
                    $post_categories = wp_get_post_categories( $page );
                    foreach ($excluded_category_ids as $category_id ) {
                        if ( in_array( $category_id, $post_categories ) ) {
                            $query->set( 'p', -$category_id );
                            break;
                        }
                    }
                }
            }
        }
    }
    add_action( 'pre_get_posts', 'exclude_post_by_category' );

    Yep, that was it…and a little more complicated than I thought. This seems to work for me using different permalink structures:

    function exclude_single_post( $query ) {
        $excluded_category_ids = array(1);
        if ( $query->is_single() ) {
            if ( ( $query->query_vars['p'] ) ) {
                $page = $query->query_vars['p'];
            } else if ( isset( $query->query_vars['name'] ) ) {
                $page_slug = $query->query_vars['name'];
                $post_type = 'post';
                global $wpdb;
                $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
            }
    
            if ( $page ) {
                $post_categories = wp_get_post_categories( $page );
                foreach ($excluded_category_ids as $category_id ) {
                    if ( in_array( $category_id, $post_categories ) ) {
                        $query->set( 'p', -$category_id );
                        break;
                    }
                }
            }
        }
    }
    add_action( 'pre_get_posts', 'exclude_single_post' );
Viewing 15 replies - 256 through 270 (of 520 total)