Adding a function to wordpress theme to bypass/replace premalinks - wordpress

Looking at Cssremix.com
when you hover over a item you can see the "Views" function I'm assuming they are using a plugin that links with the post views but when clicked the item redirects not to the post but to a website/site used

This plugin gives you the post views. To filter a link you can hook into certain filters, such as post_link or the_permalink.
Here are some docs:
Plugin API usage
Filter reference
The usage would be something like this:
add_action('post_link', 'do_custom_link');
function do_custom_link($url, $post) {
$external_url = get_post_meta($post->ID, 'external_url', true);
if($external_url) {
return $external_url;
}
return $url;
}
This would get the external url from a meta field stored with the post, called external_url. You would define that meta field using the custom fields UI when you create the post through the admin pages.

Related

Custom Columns Don't Display Anything

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.
It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.
This is what it looks like when viewing the custom post type
and
this is what it looks like when viewing a regular core post.
// Add post ID column to use an order ID in all posts view.
add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);
function oms_order_id_header($columns) {
//Remove title column
//unset($columns['title']);
//Add new columns
$columns['order_id_header'] = 'Order ID';
$columns['customer_header'] = 'Customer';
$columns['author'] = 'Owner';
return $columns;
}
function oms_order_id_column( $column, $post_id ) {
if ($column == 'order_id_header') {
echo $post_id;
}
}
This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.
Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.
I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.
Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot:
Try printing "hello world" instead of the $post_id to check if it prints in all cases.

Redirecting from Custom Admin Page Produces "Headers already sent"

I've registered a custom Admin page in my plugin through add_submenu_page. In the callback function (the one that generates the contents of the admin page), I have the following code:
wp_redirect('http://google.com');
exit;
However, when I visit the admin page I get an error:
Warning: Cannot modify header information - headers already sent by (output started at ..\wp-admin\includes\template.php:1637) in ..\wp-includes\pluggable.php on line 878
The callback from add_submenu_page happens too late (after the admin sidebar and header are rendered), this is why the location header can not be sent anymore.
To accomplish this, we need to hook a function a bit earlier in the WordPress admin area, before the headers are sent (e.g. admin_init).
A good way:
function myplugin_preprocess_pages($value){
global $pagenow;
$page = (isset($_REQUEST['page']) ? $_REQUEST['page'] : false);
if($pagenow=='admin.php' && $page=='myplugin-custom-page-slug'){
wp_redirect('http://google.com');
exit;
}
}
add_action('admin_init', 'myplugin_preprocess_pages');
The above code will redirect you to Google whenever you try to view wp-admin/admin.php?page=myplugin-custom-page-slug.
In my case, I've attached the custom page via add_submenu_page to the default (admin.php) parent in the Admin area and I've set the custom page's slug to myplugin-custom-page-slug. Feel free to replace the values in the code above or even add a PHP switch if you have a lot of custom admin pages.
This way we have hooked early enough to do a redirection whenever our custom admin page is viewed.
Update: (A different approach)
Thanks to this post, I've learned that WordPress creates a unique action that you can hook to for each custom admin page (load-{parent_page_slug}_page_{plugin_subpage_slug}). For example, if you've added a custom admin page with parent admin.php and slug myplugin-custom-page, you can hook to its "load" action in the following manner:
add_action( 'load-admin_page_myplugin-custom-page', 'myplugin_custom_page_redirect' );
function myplugin_custom_page_redirect() {
if ( 'myplugin-custom-page' == filter_input( INPUT_GET, 'page' ) ) {
wp_redirect( 'http://google.com' );
exit;
}
}
Note that the action name has some things to consider. It's a mixture of underscores and dashes and make sure you only include the parent page's name without the extension (so "admin" instead of "admin.php")

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

display custom fields automatically when a custom post type is displayed

I am trying to automatically display all the custom fields of a custom post type alongside it's title and content.(Not in admin but on my actual site)
I need to be able to do this with an action hook or filter, rather than creating a template.
After scouring the web I was able to find the 'publish_{custom_post_type_name}' hook:
function my_cool_hook() {
echo get_post_meta($post->ID, 'my-custom-field-name', true);
}
add_action( 'publish_past_symposia', 'my_cool_hook' );
but it doesn't seem to do anything when I view my published custom post type on my site. Any ideas?
add_action( 'publish_past_symposia', 'my_cool_hook' );
This hook triggered only if PUBLISH post type.
YOu need to trigger the hook on web part - so...
add_filter('the_content', 'my_cool_hook');
function my_cool_hook($content){
return $content.get_post_meta(get_the_id(), 'my-custom-field-name', true);
}
now the content body filtred and your string from custom fields added.

Wordpress: add custom page attribute

I have the NextGEN Gallery plugin on my wordpress site. Normally I would add this short code in my page content to display gallery items:
[ nggallery id=5 template=custom ]
Now I'd like to replace this by adding custom fields in the Page Attributes setting when you are adding/editing a page. The custom fields would be "Gallery ID" and "Template name".
I'm of course using a custom page template. How can I retrieve the page attributes into this page template?
Thanks in advance!
Yes, what Stratboy said. Documentation here. This is the setup that should work for you:
<?php
$gallery_ID = get_post_meta($post->ID, 'Gallery ID', true);
$template_name = get_post_meta($post->ID, 'Template Name', true);
if ($gallery_ID && $template_name){
//echo '$gallery_ID: '.$gallery_ID.'; $template_name: '.$template_name.';';
echo do_shortcode('[nggallery id="'.$gallery_ID.'" template="'.$template_name.'"]');
}
?>
So, first:
have you already implemented the custom page template?
have you already implemented the custom fields?
Anyway, generally, in a template you get your custom fields values using the get_post_meta function in the Loop, like this:
//the last param tell if you want the value returned as a string (true) or an array (false)
get_post_meta($post->ID,'field name',true);
You can echo the returned value or use it for other tasks.
Let me know.

Resources