Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using wp_insert_link to import a lot of links, but I have to assign the link category manually afterwards. Is there a corresponding Wordpress function to create link categories?

share|improve this question
add comment

1 Answer

wp_insert_link() allows you to set the category in the $linkdata args.

<?php
$linkdata = array(
    "link_id"       => 0, // integer, if updating, the ID of the existing link
    "link_url"      => '', // varchar, the URL the link points to
    "link_name"     => '', // varchar, the title of the link
    "link_image"        => '', // varchar, a URL of an image
    "link_target"       => '', // varchar, the target element for the anchor tag
    "link_description"  => '', // varchar, a short description of the link
    "link_visible"      => 'Y', // varchar, Y means visible, anything else means not
    "link_owner"        => '', // integer, a user ID
    "link_rating"       => 0, // integer, a rating for the link
    "link_updated"      => '0000-00-00 00:00:00', // datetime, when the link was last updated
    "link_rel"      => '', // varchar, a relationship of the link to you
    "link_notes"        => '', // text, an extended description of or notes on the link
    "link_rss"      => '', // varchar, a URL of an associated RSS feed
    "link_category"     => '' // int, the term ID of the link category. if empty, uses default link category
);
?>

This function does not create the link category it only assigns the link to category you specify (using the category id), so you will need to create all your link categories first then run your code to insert.

I would just set up all the links you want to import into arrays then run through them with a foreach loop.

$links = array(
    'Link Title' => array('http://example.org', 23 ),
    'Another Link Title' => array( 'http://domain.org', 17),
);

foreach ( $links as $key => $value ) {
   wp_insert_link( array( 'link_name' => $key, 'link_url' => $value[0], 'link_category' => $value[1] ) );

}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.