WordPress.org

Ready to get started?Download WordPress

Forums

Path
Custom post types problem and "fatal error: require_once() " message (10 posts)

  1. gombroo
    Member
    Posted 1 year ago #

    Hello,

    I've registered some custom post types in my child theme. They are included in the directory: libs/posttypes.php and looks like this:

    <?php
    add_action('init', 'mamepro_init_posttypes');
    function mamepro_init_posttypes() {
    
    /* Register Announcements  */
            $announcements_args = array(
                'labels' => array(
                    'name' => 'Ogłoszenia',
                    'singular_name' => 'Ogloszenie',
                    'all_items' => 'Wszystkie ogłoszenia',
                    'add_new' => 'Dodaj nowe ogłoszenie',
                    'add_new_item' => 'Dodaj nowe ogłoszenie',
                    'edit_item' => 'Edytuj ogłoszenie',
                    'new_item' => 'Nowe ogłoszenie',
                    'view_item' => 'Zobacz ogłoszenie',
                    'search_items' => 'Szukaj w ogłoszeniach',
                    'not_found' =>  'Nie znaleziono żadnych ogłoszeń',
                    'not_found_in_trash' => 'Nie znaleziono żadnych ogłoszeń w koszu',
                    'parent_item_colon' => ''
                ),
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'query_var' => true,
                'rewrite' => true,
                'capability_type' => 'post',
                'hierarchical' => false,
                'menu_position' => 5,
                'supports' => array(
                    'title','editor','author','thumbnail','excerpt','comments','custom-fields'
                ),
                'has_archive' => true
            );
    
            register_post_type('announcements', $announcements_args);
    }
    ?>

    I added a function to funcions.php file located in my child theme directory to make it work:

    require_once MAMEPRO_THEME_DIR.'libs/posttypes.php';

    But as a result I got an error message:

    Warning: require_once(/home/marszand/domains/mamepro.org/public_html/wp-content/themes/path/libs/posttypes.php) [function.require-once]: failed to open stream: No such file or directory in /home/marszand/domains/mamepro.org/public_html/wp-content/themes/mamepro/functions.php on line 11

    Fatal error: require_once() [function.require]: Failed opening required '/home/marszand/domains/mamepro.org/public_html/wp-content/themes/path/libs/posttypes.php' (include_path='.:/usr/local/php/p53/lib/php') in /home/marszand/domains/mamepro.org/public_html/wp-content/themes/mamepro/functions.php on line 11

    I wonder why wordpress can't find posttypes.php and funcions.php files. If you can help me I'd be grateful!

    Thanks in advance!

  2. paulwpxp
    Font hero
    Posted 1 year ago #

    Reading from the error message, this MAMEPRO_THEME_DIR doesn't point to your child theme's directory(folder). It's pointing to parent theme's directory and couldn't find such file.

  3. gombroo
    Member
    Posted 1 year ago #

    Thanks! I could noticed it so far.

    The problem is: how can I change it? Which parent file I should edit and which function should I use?

    I'm a complete beginner in php, so any advice will be helpful!

  4. paulwpxp
    Font hero
    Posted 1 year ago #

    None of the parent theme files should be edited at all. Everything goes in child theme.

    get_stylesheet_directory()
    http://codex.wordpress.org/Function_Reference/get_stylesheet_directory

    require( get_stylesheet_directory() . '/libs/posttypes.php' );

    That is in your child theme's setup function, and you have posttypes.php inside child folder like this mamepro/libs/posttypes.php

  5. gombroo
    Member
    Posted 1 year ago #

    My child-theme functions.php which causes this error looks like this:

    if(!defined('MAMEPRO_THEME_DIR')) {
    define('MAMEPRO_THEME_DIR',get_theme_root().'/'.get_template().'/');
    }
    
    if(!defined('MAMEPRO_THEME_URL')) {
    define('MAMEPRO_THEME_URL',WP_CONTENT_URL.'/themes/'.get_template().'/');
    }
    require_once MAMEPRO_THEME_DIR.'libs/posttypes.php';

    I tried to replace this:

    require_once MAMEPRO_THEME_DIR.'libs/posttypes.php';

    with this:

    require( get_stylesheet_directory() . '/libs/posttypes.php' );

    but still I got some syntax error.

  6. paulwpxp
    Font hero
    Posted 1 year ago #

    but still I got some syntax error.

    "some" or "same" ? and what the error message is saying ?

  7. Sami Keijonen
    WordPress-asiantuntija
    Theme Author

    Posted 1 year ago #

    Before you go on, you should not register your custom post type in a theme. It belongs in a custom plugin because you're not gonna use Path theme forever.

    You can make your custom plugin with this.

    http://wordpress.org/plugins/pluginception/

    Or if you want to learn more about how to make a plugin, I'd start with this.

    https://codex.wordpress.org/Writing_a_Plugin

  8. gombroo
    Member
    Posted 1 year ago #

    @paulwpxp this error says:

    Parse error: syntax error, unexpected '}' in /home/marszand/domains/mamepro.org/public_html/wp-content/themes/mamepro/libs/posttypes.php on line 114

    Nothing changes when I try to remove or put this bracket in another place.

    The whole posttypes.php looks like this:

    <?php
    if(!defined('MAMEPRO_THEME_DIR')) {	define('MAMEPRO_THEME_DIR',get_theme_root().'/'.get_template().'/');
    }
    if(!defined('MAMEPRO_THEME_URL')) {
    	define('MAMEPRO_THEME_URL',WP_CONTENT_URL.'/themes/'.get_template().'/');
    }
    require( get_stylesheet_directory() . '/libs/posttypes.php' );
    ?>
  9. paulwpxp
    Font hero
    Posted 1 year ago #

    Now the error is not about pointing to the wrong folder/file. It's now the syntax error in posttypes.php. So please check it there.

    The code you posted above should NOT be from posttypes.php, it should be from functions.php. Also, since now we are using get_stylesheet_directory() to get to that file, so there is no need to define those constants, unless you need to call them somewhere else.

  10. Sami Keijonen
    WordPress-asiantuntija
    Theme Author

    Posted 1 year ago #

    Register Code should be in a plugin.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic