• I have a php script taking categories from softonic.com, and im trying to insert them into my blog, but I have a few problems. The first of all, is that the categories are not with any link, so you can’t click on them. Can look at it right here: http://www.softcnet.com As you can see in the main menu there are categories but you cannot click on them. But my main problem is my script is not inserting all the categories that it has to insert, and it doesn’t throw any clear errors, so I’m not able to figure out where is the problem. In fact, the only error I have, and is thrown 570 times is this one:

    Notice: Object of class WP_Error could not be converted to int in /route/wp-includes/post.php on line 2157.

    I’ve seen my database in wp_term_relationships and there is 577 new entries and the error is thrown 570 times, so its thrown almost allways, but I don’t know why because I’ve tested my code and it looks fine. I’ve looked at google for this error but haven’t found anything useful. I need to fix this script because I can’t run my next script if I don’t have the categories fist.

    here is my php script:

    <?php
    ini_set('memory_limit', '-1');
    set_time_limit(0);
    //Needed to create categories
    include("../wp-admin/includes/taxonomy.php");
    
    //Needed to create new posts
    include ("../wp-load.php"); 
    
    //Needed to create navigation menu
    include ("wp-includes/nav-menu.php");
    
    //display errors
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    libxml_use_internal_errors(true);
    
    /**
    Softonic bot to download categories/subcategories from softonic and insert/create them in SoftCnet
    */
    //Bot to take categories from softonic
    $url = array();
    
    //Prepare categories array Url
    $url[] = 'http://en.softonic.com/mac/categories'; //5685
    $url[] = 'http://en.softonic.com/windows/categories';//5683
    $url[] = 'http://en.softonic.com/android/categories';//5684
    $url[] = 'http://en.softonic.com/iphone/categories';//5686
    $url[] = 'http://en.softonic.com/web-apps/categories';//5688
    $url[] = 'http://en.softonic.com/java/categories';//5689
    $url[] = 'http://en.softonic.com/symbian/categories';//5690
    $url[] = 'http://en.softonic.com/windows-phone-7/categories';//5691
    $url[] = 'http://en.softonic.com/blackberry/categories';//5692
    $url[] = 'http://en.softonic.com/windowsmobile/categories';//5693
    $url[] = 'http://en.softonic.com/palm/categories';//5694
    $url[] = 'http://en.softonic.com/pocketpc/categories';//5695
    
    //Curl initialization
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEFILE, '');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2450.0 Iron/46.0.2450.0 Safari/537.36');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    //domDocument creation
    $doc = new DOMDocument();
    $temp_dom = new DOMDocument();
    libxml_use_internal_errors(true);
    
    /**
    Create menu
    */
    
    // Check if the menu exists and create it
    $menu_name = 'Main Menu';
    $menu_exists = wp_get_nav_menu_object( $menu_name );
    if( !$menu_exists){
    
        $menu_id = wp_create_nav_menu($menu_name);
        $menu = get_term_by( 'name', $menu_name, 'nav_menu' );
    
    }
    
    /**
    Create Apps category: need error handling, if category exist it throws an error!
    */
    $pCat = wp_create_category('Apps');
    $parent = wp_update_nav_menu_item($menu->term_id, 0, array(
            'menu-item-title' => 'Apps',
            'menu-item-object-id' => 0,
            'menu-item-db-id' => 0,
            'menu-item-object' => 'category',
            'menu-item-parent-id' => 0,
            'menu-item-type' => 'taxonomy',
            'menu-item-url' => 0,
            'menu-item-status' => 'publish',)
        );
    
    /**
    Create more parent
    */
    $mpCat = wp_create_category('More',$pCat);
    $moreParent = wp_update_nav_menu_item($menu->term_id, 0, array(
            'menu-item-title' => 'More',
            'menu-item-object-id' => 0,
            'menu-item-db-id' => 0,
            'menu-item-object' => 'category',
            'menu-item-parent-id' => $parent,
            'menu-item-type' => 'taxonomy',
            'menu-item-url' => 0,
            'menu-item-status' => 'publish',)
        );
    
    $mainCat = array('mac','windows','android','iphone','web-apps','java','symbian','windows-phone-7','blackberry','windowsmobile','palm','pocketpc');
    
    //create Apps category
    //$id = wp_create_category('Apps');
    
    //Retrieves all urls in url array and connect to them
    foreach ($url as $key=>$link)
    {
    
        if($key<=3){
            $mainParent=wp_update_nav_menu_item($menu->term_id, 0, array(
                    'menu-item-title' => $mainCat[$key],
                    'menu-item-object-id' => 0,
                    'menu-item-db-id' => 0,
                    'menu-item-object' => 'category',
                    'menu-item-parent-id' => $parent,
                    'menu-item-type' => 'taxonomy',
                    'menu-item-url' => 0,
                    'menu-item-status' => 'publish')
                );
            $pCat2=wp_create_category($mainCat[$key], $pCat);
    
        }else{
    
            $mainParent=wp_update_nav_menu_item($menu->term_id, 0, array(
                            'menu-item-title' => $mainCat[$key],
                            'menu-item-object-id' => 0,
                            'menu-item-db-id' => 0,
                            'menu-item-object' => 'category',
                            'menu-item-parent-id' => $moreParent,
                            'menu-item-type' => 'taxonomy',
                            'menu-item-url' => 0,
                            'menu-item-status' => 'publish')
                        );
            $pCat2=wp_create_category($mainCat[$key], $mpCat );
        }
    
        curl_setopt($ch, CURLOPT_URL, $link);
        $result = curl_exec($ch);
    
        //Creates Dom
        $doc->loadHtml($result);
        $xpath = new DOMXPath($doc);
        // libxml_clear_errors();
    
        //Do html query
        $classname = 'categories_group';
        $xlink  = $xpath->query("//*[contains(@class, '$classname')]");
    
        //Parse result
        foreach($xlink as $k=>$x)
        {
    
            //GET CATEGORIES
            foreach($x->getElementsByTagName('strong') as $key => $l)
            {
                $cat_name = $l->textContent;
                $id = createCategoriesNavMenu($menu,$mainParent,$cat_name);
                $mpsCat=wp_create_category($cat_name, $pCat2 );
    
            }
            //GET SUBCATEGORIES
            foreach($x->getElementsByTagName('li') as $key => $l)
            {
                $subcat_name = $l->textContent;
                createCategoriesNavMenu($menu,$id,$subcat_name);
                wp_create_category($subcat_name, $mpsCat);
            }
        }
    }
    
    //Set subCategory
    function createCategoriesNavMenu($menu,$parent,$title){
    
        $id = wp_update_nav_menu_item($menu->term_id, 0, array(
                'menu-item-title' => $title,
                'menu-item-object-id' => 0,
                'menu-item-db-id' => 0,
                'menu-item-object' => 'category',
                'menu-item-parent-id' => $parent,
                'menu-item-type' => 'taxonomy',
                'menu-item-url' => 0,
                'menu-item-status' => 'publish')
            );
        return($id);
    }
    ?>
Viewing 1 replies (of 1 total)
  • Well, that error message is giving you a very important clue:

    Something that you’re trying to do is producing a WordPress Error object (a WP_Error), instead of a category, menu item, post, etc. Instead of checking for the error object, you’re assuming that the object was correctly created, and trying to use it.

    You should go through all of the functions that you’re calling (such as wp_create_category) and check the output of these funcitons – some of them will produce “0” if they are not successful, some of them will produce a WP_Error object.

    For each of these functions, your code needs to check for failure, instead of assuming success.

    For example, wp_create_category returns “0” if the category was not created, so you would do this:

    ...
    $pCat = wp_create_category('Apps');
    if ($pCat){
      [other stuff you are doing w/ $pCat]
    } else {
      [the category was not created.  Why?  Maybe it was there before?]
    }
Viewing 1 replies (of 1 total)

The topic ‘script php stop inserting categories’ is closed to new replies.