コード例 #1
0
 public function test_is_new_page()
 {
     $referer = 'http://www.data.gov/wp/wp-admin/post-new.php?post_type=page';
     $this->assertTrue(datagov_custom_is_new_page($referer));
     $referer = 'http://dev-datagov.reisys.com/wp/wp-admin/post-new.php';
     $this->assertFalse(datagov_custom_is_new_page($referer));
     $referer = 'http://www.data.gov/wp/wp-admin/post.php?post=40739&action=edit';
     $this->assertFalse(datagov_custom_is_new_page($referer));
 }
コード例 #2
0
/**
 * This hook function that adds custom_permalink metadata to pages
 * in the following format: (%category%/%post_name%). This only works
 * for pages for which you don't explicitly set a permalink value.
 * In short, it auto populates the permalink field if left empty.
 *
 * @param int post_id
 *   The id of the page
 *
 */
function datagov_custom_add_category($post_id)
{
    // If our form has not been submitted, we dont want to do anything
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // ignore wp_insert_post action that is called when the page
    // is in revision mode
    if (wp_is_post_revision($post_id)) {
        return;
    }
    // ignore wp_insert_post action that is called after
    // user clicks Add New Page
    if (empty($_POST)) {
        return;
    }
    $custom_permalink = get_post_meta($post_id, 'custom_permalink', true);
    $permalink = get_permalink($post_id);
    $is_new_page = datagov_custom_is_new_page($_POST['_wp_http_referer']);
    if (empty($_POST['custom_permalink']) && empty($custom_permalink) && $is_new_page) {
        $post_terms = $_POST['post_category'];
        if (!empty($post_terms)) {
            // add category(s) slug only to new pages
            $post_term_id = end($post_terms);
            $term_exists = term_exists((int) $post_term_id, "category");
            $is_term = $term_exists !== 0 && $term_exists !== NULL;
            if ($is_term) {
                $post_term = get_term((int) $post_term_id, "category");
                $term_hirarchy = datagov_custom_term_hirarchy($post_term);
                $term_hirarchy = implode("/", array_reverse($term_hirarchy));
                $custom_permalink = str_replace(home_url(), $term_hirarchy, $permalink);
                add_post_meta($post_id, 'custom_permalink', $custom_permalink);
            }
        }
    }
}