Automatically select parent page if custom field equals page title - wordpress

Is it possible to automatically set/select a parent page on page creation when a custom field and page title are equal?
Example:
I have the following page hierarchy:
Providers
Provider Page (Page title = user1)
Provider Package
When a new page is created, and the custom field value is “user1” and there is a page with the title “user1”. Then the Provider Page, in this case “user1”, should automatically be set as the parent for the page that is created (Provider Package page).
I hope it is a bit clear because it is quite difficult to explain I notice myself.
Is such a thing possible?

You can achieve this by using post editing hook filters/actions. Like this
add_action( 'edit_post', 'parentsetter_save_post' );
function parentsetter_save_post()
{
global $post;
$custom_field=get_post_meta($post->ID,'customfieldname',true);
if ($custom_field!=''){
$parent_page=get_page_by_title($custom_field);
if (!empty($parent_page) and $post->post_parent!=$parent_page->ID){
global $wpdb;
$wpdb->query($wpdb->prepare("update $wpdb->posts set post_parent=%d
where ID=%d",$parent_page->ID,$post->ID));
}
}
}
Just replace "customfieldname" with your custom field name and it will work.

Related

possible woocommce bug keep showing "total_sales" in metabox "custom field" add/edit product page?

As shown in the edit/add product page, this item "total_sales" always show up in the default metabox "custom field".
It's not doing any harm yet but it's annoying.
However, someone might edit the total sales number by accident and would cause problems.
I am writing a child theme from storeFront.
Is this a bug in woocommerce or did I accidentally changed something somewhere that causes this?
This is by default. If you think this is a bug then you can always open a thread on https://github.com/woocommerce/woocommerce/issues
EDIT
Custom fields or post meta entries can be hidden from the by default in Wordpress available »Custom Fields«-Metabox by prefixing them with a underscore - _ - as noted here.
If you want to hide the field you can use this piece of code that i wrote for you
function filter_is_protected_meta( $protected, $meta_key, $meta_type ) {
if ( $meta_key == 'total_sales' ) {
$protected = true;
}
return $protected;
}
add_filter( 'is_protected_meta', 'filter_is_protected_meta', 10, 3 );

How to add page URL to list of pages in WordPress admin panel?

Currently, by default, the following columns appear in the list of pages in the WordPress admin panel:
Title
Author
Comments
Date
and because I have AIO SEO installed:
SEO Title
SEO Description
SEO Keywords
Is there a way to have WordPress also display the URL to the page (at least the part of the URL that is created when the page itself is created)?
The page url is actually already there by default, it's just hiding. When you hover over a page title, several links appear below the title -- edit, quick edit, trash, view. View is the hyperlink to the page, which you can click to view the page, or right click and copy the link address to use elsewhere.
Otherwise, if you are using a custom/child theme, you could add the following to your functions.php file:
add_filter('manage_page_posts_columns', 'my_custom_column', 10);
add_action('manage_page_posts_custom_column', 'add_my_custom_column', 10, 2);
function my_custom_column($defaults) {
$defaults['url'] = 'URL';
return $defaults;
}
function add_my_custom_column($column_name, $post_id) {
if ($column_name == 'url') {
echo get_permalink( $post_id );
}
}
Note: This just creates a text url to your page.
Also note, you do not want to edit your functions.php file directly if you are using a theme you did not create, as it will be overwritten when you update. If you want to add this to an existing theme, I'd suggest looking into child themes.
This is helpful. I would only improve the output slightly by removing the site url and just showing the page. Takes up less space and less to weed through visually.
if ($column_name == 'url') {
$siteURL=get_site_url($post_id);
$link= get_permalink( $post_id );
echo str_replace($siteURL,"",$link);
}

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

Adding a function to wordpress theme to bypass/replace premalinks

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.

Resources