How to speed up IMDb ratings plugin in WP? - wordpress

I tried to implement IMDb ratings plugin in WP, in this way:
I made a meta box and I copy in it the HTML code generated by the IMDb ratings plugin from IMDb site, here is the code generated by plugin, but when loading the website, plugin content is very slow (a few seconds).
Meta box:
array(
'label' => 'iMDb Rating',
'desc' => 'iMDb Rating Script',
'id' => 'imdb-rating',
'priority' => 'high',
'type' => 'textarea',
'std' => ''
),
Display meta box method in post:
<?php $test_field_value = get_post_meta( get_the_ID(), 'imdb-rating', true ); echo $test_field_value; ?>
Question:
Is there a way to make the plugin to load faster, or to implement it in a good way to avoid the slow display?
I asked on wordpress.stackexchange too, but i was redirected here.

2 ways I can suggest:
Load the IMDB data using AJAX, that way page will render first and then you can load the rest of the data. You could even create a custom wordpress page in your theme that loads the plugin and call this from your on page js.
Cache the data so that you only reload on a regular basis. Ive not used the plugin so my guess is you would probably need to do a little rewriting to get that happening.

Related

Order archive posts by number of social shares

I have Yoast Seo plugin installed into my website and this brings back the number of social shares per post and stores it in the database.
Is it possible to change the archive page to order the posts by the number of social shares?
I've tried a few things listed here but can't seem to get it to work
<?php
/* The loop */
while ( have_posts() ) : the_post();
include( locate_template( 'content-' . $layout_this . '.php' ) );
endwhile;
?>
This one might be tougher than you think. Yoast SEO does not actually store the number of times any given social media outlet is actually shared. This is done by constantly running a check from the respective social media API and will retrieve the number of shares/likes which happened on that respective post.
Essentially what needs to be done is you'd need to hook onto the API a similar way Yoast is doing so, and then start comparing the values which are being sent back from the API in order to order your posts on the front-end by a number of most shares.
The idea for the functionality is pretty useful... I'm hoping that in the future there will be a new table added perhaps with the Yoast plugin which will actually save this data rather than having to refer to the API.
EDIT: In theory, this should be possible then if you know what meta value(s) you're working with from the database. As long as the posts have association with these values, this should be possible. You can query posts with specific meta keys and order them, too. Here's an example of what might work within your template, with some adjustments.
$args= query_posts(
array( 'post_type' => 'your post type', //for posts, enter 'post'
'order' => 'ASC',
'meta_key' => 'some_key', //the meta key
'orderby' => 'meta_value', //or 'meta_value_num'
)
);
See here as well, someone asked a very similar question : How to fetch posts order by meta value?

Any way to add metaboxes in custom settings page of custom post type in wordpress? [duplicate]

This question already has an answer here:
Add meta box to WordPress options page
(1 answer)
Closed 4 years ago.
Hi I would like to add metaboxes under custom settings page which is under a custom post type. I can create metaboxes for custom post types also I can create a theme options. But can't find any way to add the metaboxes on a custom settings page. Like my post type hierarchy is like below:
Products
- All item
- Add Item
- Product Category
- Product Settings
I want to add the metaboxes & create a options page on that settings page. Can you please guide me through this one.
I've been trying to follow this gist but can't find a way.
https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/options-and-settings-pages/theme-options-cmb.php
Also can you let me know if I can achieve something by tweaking this code where key|value operates
$cmb = new_cmb2_box( array(
'id' => $this->metabox_id,
'hookup' => false,
'show_on' => array(
// These are important, don't remove
'key' => 'options-page',
'value' => array( $this->key, )
),
) );
I've created the settings page by this code
add_submenu_page('edit.php?post_type=ch_product_showcase', 'Product Showcase Settings', 'Showcase Settings', 'edit_posts', basename(__FILE__), array( $this, 'chProductShowcaseSettingsOptions') );
I've done it many times. Use this code and tweak it to your needs:
https://gist.github.com/turtlepod/5203512
Found as a link in the comments of this page:
https://gist.github.com/bueltge/757903
Originally posted here:
Wordpress - Add meta box to options page
Normally I don't like answering with links to another site, but in this case the code is on gist and hopefully will never go away!

Mixing Wordpress 3.7.1 Page Templates and Custom Post Types

Overview
I have a Custom Post Type called Locations. When the user visits /locations, I would like them to see some general information about all the locations. When the user visits /location/{a-location}, I would like them to see specific information about "a-location". Sounds easy enough.
1st Solution Attempt
I created a Locations page in the Wordpress dashboard to hold the content for the /locations url. I then created an archive-locations.php file and a single-location.php. archive-locations.php pulls info out of the Locations page and it lists all of the custom post type Locations. When the user clicks on a link for a location, the user gets redirected to /locations/{a-location} and single-location.php gets called. This almost does the trick, but it just doesn't feel right and it's creating some other problems.
The Problem
When a user visits /locations I would prefer to be working with a page template for the Locations page (instead of the archive-locations.php page). I'm using the Yoast plugin to specify a custom meta title and description for each page. Since archive-locations.php is being used, all of this meta gets ignored.
2nd Solution Attempt
So I created a page-locations.php template for the Locations page and I was hoping that it would get called instead of the archive-locations.php. Unfortunately, it does not. I then tried removing the archive-locations.php thinking that maybe it was taking precedence. That didn't help either; Wordpress simply renders index.php. If I disable the Custom Post type and visit /locations, then my page template is called correctly. It looks like naming a custom post type the same name as a page causes Wordpress some issues. However, I need to be somewhat similar for the URL structures to work out. /locations needs to pull from a page template; /locations/{a-location} needs to pull from another php pfile.
Question
Is there any way to get page templates and custom post types with similar slugs to work together? If not, I guess my only option is to enhance my header.php and be smarter about determining the title and description when I'm on /locations.
Here's the code that registers my custom post type in case I'm doing something wrong:
$args = array(
'labels' => $labels,
'description' => 'Holds our location specific data',
'public' => true,
'menu_position' => 20,
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes' ),
'hierarchical' => true,
'has_archive' => true,
'rewrite' => array('with_front' => false, 'slug' => 'locations') //remove /news/ from the permalink when displaying a custom post type
);
register_post_type( 'locations', $args );
(I also tried changing has_archive to false, but it didn't help.)
What you are trying to do, I once managed to get done easily. I mean having custom post type and use its slug with cusotm page.
Did you try to flush rewrite rules? (just visit Permalink page and save the rules as they are)
So I created a page-locations.php template for the Locations page and I was hoping that it would get called instead of the archive-locations.php. Unfortunately, it does not. I then tried removing the archive-locations.php thinking that maybe it was taking precedence. That didn't help either; Wordpress simply renders index.php
You do not write, how do you load your page template. Do you rely on automatic WP connection of pageslug and page-pageslug.php or you set page template through Page attributes menu?

Custom Post Type Admin Post Status Not Working

For the life of me, I can not figure this out.
I set up a custom post type in WordPress like so:
register_post_type( 'myposttype',
array(
'labels' => array(
'name' => __( 'MyPostTypeName' ),
'singular_name' => __( 'MyPostTypeName' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'custom-fields')
)
);
When I first started I added add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 ); with some basic capabilities. I think I may have set that part up wrong, but I quickly got rid of it and returned the post type to normal capabilities.
Now whenever I try to access the custom post type in my admin, I run into some weird problems. Namely, no matter what Post status I click on (Published, Drafts, Private, etc.) I only get published posts. Also sorting the columns and other similar admin functions don't work at all. It's like edit.php is completely broken, but only for this specific custom post type. It works just fine on all other custom post types.
I've tried everything, right down to deleting every mention of the custom post type from the admin panel and then trying to register it again, and nothing works. I really need to keep the slug from the custom post type the same, but I'd be open to registering a new post type, moving posts over and then changing the slug I just can't figure out a good way to do it.
Anyway, I'd appreciate any help in figuring out how to fix the custom post type admin panel.
Thanks
Do you have a "posts_orderby" filter added anywhere?

Wordpress Sticky Posts with Custom Post Types

So i need the ability to have a featured or "sticky" post in wordpress, and it occurred to me! Why not use the Sticky Posts facility, but after doing a bit of reading it seems that Wordpress decided to not include support for it in latest releases and they don't seem to be pushing any solution for future releases.
Now that leaves me in a predicament i wish to have the ability to have a featured post or custom post without using a category of such.
I've also seen a few people state they have hacked wordpress with possibly a function to add the ability of sticky posts to custom post types, shame they didn't share the source!
How would this be done?
You can do it with a custom field (post_meta) on the custom post type. Then fire a custom query that selects for the meta_value:
$args = array('post_type' => 'my_custom_post_type', 'post_status' => 'publish', 'meta_query' => array('relation' => 'AND', array('key' => 'is_sticky', 'value' => '1', 'compare' => '=', 'type' => 'CHAR')));
$sticky_posts = new WP_Query($args);
Should return an array of published posts of post_type: my_custom_post_type that have the sticky flag set.
Although I haven't tested the above code, I'm doing something similar and it works fine.
You can use this plugin, it has it's own limitations, but works pretty well if you don't need something elaborate.
You can save a custom meta with the name of "sticky" and add it the value "on" when the post is sticky. That can be done with a custom metabox and a checkbox.
Wordpress will automatically add the word "Sticky" on the backend posts listing table
You can retrieve a loop with your sticky custom posts by adding the values 'meta_key' => 'sticky' and 'meta_value' => 'on' to the args of your query
I posted a working solution as of WordPress 4.2 here:
https://wordpress.stackexchange.com/questions/90958/adding-sticky-functionality-to-custom-post-type-archives/185915#185915
Basically, it implies installing a small plugin and add a code snippet.
I have Wordpress 3.2.1, the latest version and I can sticky posts. It works for me on my site.

Resources