Showing posts with label WORDPRESS. Show all posts
Showing posts with label WORDPRESS. Show all posts
0

Wordpress Ajax Json callimg



IN functions.php 

add_action('wp_footer', 'eps_footer');
function eps_footer() {
    echo "<script>var ajax_request_url = '".admin_url( 'admin-ajax.php' )."'</script>";
}



$.get( ajax_request_url,
  {
   'action' : 'get_post_title',
   'post_id' : 1
  }, function( response ){
    if ( !response.error ) {
      alert ('AJAX request made! The post title is ' + response.post_title );
    } else {
      alert ('error: ' + response.error );
    }
},  "json" );





add_action( 'wp_ajax_nopriv_get_post_title', 'get_post_title' );
add_action( 'wp_ajax_get_post_title', 'get_post_title' );

function get_post_title() {
  if ( isset($_GET['post_id']) ){
    $id = (int) $_GET['post_id'];
    $post = get_post( $id );
    header( "Content-Type: application/json" );
    echo json_encode( array('post_title' => $post->post_title) );
  } else {
    header( "Content-Type: application/json" );
    echo json_encode( array('error' => 'bad request') );
  }
  exit;
}

Read
0

Where is the right place to register/enqueue scripts & styles

I am using WordPress 3.1.4 by now. I am confused with where (which hook) do I use:
  • to register and/or enqueue
  • scripts and styles
  • on front- and back-ends?
Questions:
  • Which are right hooks to use?
  • All front end register/enqueue scripts/styles in init?
  • Why is there no admin_print_styles-{xxx}?
Read
0

Create more Meta Boxes as needed

I'd like users to be able to create and remove additional meta box fields as needed.
For example, say a music podcast with a variable amount of songs played per episode. The user should be able to click a button that will add additional fields to enter each song as needed.
Ideally this would be done without the use of a plugin, but coded into the functions file.
Read
0

Custom post types, taxonomies, and permalinks

I simply want my URLs to look like this:
mysite.com/products/category1/product-name1 
mysite.com/products/category2/product-name2
But for the life of me, no matter what I do, I'm getting the dreaded 404 issue. Pages work okay, and Posts work okay, but my custom posts don't work correctly. They're showing up as:
mysite.com/products/product-name1
mysite.com/products/product-name2
Which actually works! It's just that I want to see my custom taxonomy in there, PLUS, I want to be able to access the taxonomy.php template I have setup by going to:
mysite.com/products/category1/
mysite.com/products/category2/
None of my slugs are the same, nor do I want them to be. Here is the post type and taxonomy part of my functions.php file:
///// CUSTOM POST TYPES /////


// register the new post type
    register_post_type( 'product_listing',
        array( 'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' ),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Create New Product' ),
            'edit' => __( 'Edit' ),
            'edit_item' => __( 'Edit Product' ),
            'new_item' => __( 'New Product' ),
            'view' => __( 'View Products' ),
            'view_item' => __( 'View Product' ),
            'search_items' => __( 'Search Products' ),
            'not_found' => __( 'No products found' ),
            'not_found_in_trash' => __( 'No products found in trash' ),
            'parent' => __( 'Parent Product' ),
            ),
            'description' => __( 'This is where you can create new products on your site.' ),
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'menu_position' => 2,
            'menu_icon' => get_stylesheet_directory_uri() . '/images/tag_orange.png',
            'hierarchical' => true,
          '_builtin' => false, // It's a custom post type, not built in!
            'rewrite' => array( 'slug' => 'products', 'with_front' => true ),
            'query_var' => true,
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
            )
);



//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_product_taxonomies', 0 );
//add_action('admin_init', 'flush_rewrite_rules');

//create two taxonomies, genres and writers for the post type "book"
function create_product_taxonomies() 
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name' => _x( 'Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Categories' ),
    'all_items' => __( 'All Categories' ),
    'parent_item' => __( 'Parent Categories' ),
    'parent_item_colon' => __( 'Parent Categories:' ),
    'edit_item' => __( 'Edit Category' ), 
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
    'menu_name' => __( 'Category' ),
  );    

  register_taxonomy('product_cat',array('product_listing'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    //'rewrite' => true,
    'rewrite' => array( 'slug' => '%category%', 'with_front' => true ),
  ));

  // Add new taxonomy, NOT hierarchical (like tags)
  $labels = array(
    'name' => _x( 'Scents', 'taxonomy general name' ),
    'singular_name' => _x( 'Scent', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Scents' ),
    'popular_items' => __( 'Popular Scents' ),
    'all_items' => __( 'All Scents' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Scent' ), 
    'update_item' => __( 'Update Scent' ),
    'add_new_item' => __( 'Add New Scent' ),
    'new_item_name' => __( 'New Scent Name' ),
    'separate_items_with_commas' => __( 'Separate scents with commas' ),
    'add_or_remove_items' => __( 'Add or remove scents' ),
    'choose_from_most_used' => __( 'Choose from the most used scents' ),
    'menu_name' => __( 'Scents' ),
  ); 

  register_taxonomy('scent','product_listing',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    //'rewrite' => array( 'slug' => 'scents' ),
  ));
}
I also have another custom taxonomy of "scents" that I'd ideally like to have some kind of friendly url but I'm more open on this. I'd like to maybe access a list of all scents by going tomysite.com/products/scents but they don't have to be category specific.
Can anybody help me? I've been going crosseyed for hours. Thanks!
Read