Code star framework add metabox to a specific page - wordpress

I am using the code star framework full version. I have integrated the framework with the theme. How can I show the meta box option only for some specific pages ( like home page and about page )? Currently, the meta box options are showing all pages.

While creating metabox, you should pass "page_templates" value in array. For example, if you want to show metabox on homepage you should do:
$homePage = 'home-page';
CSF::createMetabox($homePage, array(
'title' => 'Homepage',
'post_type' => 'page',
'page_templates' => 'index.php', //filename goes here
'data_type' => 'serialize',
));
For more info: http://codestarframework.com/documentation/#/configurations?id=metabox-option-framework

Related

Custom page created by a wordpress plugin

I am trying to figure out how to create a Wordpress plugin that adds a page to the Wordpress website on which it is installed.
I came up with the following which works and adds the page.
However, it's far from what I am trying to achieve:
How to change the entire page? Not just the content? Right now, it has all the header and footer and navbar.
How to have PHP code in the page, not just static content?
Is it possible to have everything under a url (https://some-url.com/my-plugin/) routed to this same page?
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc.
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
You want wp_insert_post()
Usage:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
For example, in your $postarr if you want it to be a page (not a blog post), use 'post_type' => 'page'. For the page title 'post_title' => 'My New Page' and so on.
See: https://developer.wordpress.org/reference/functions/wp_insert_post/
Updated for additions to user question
If you don't want it to follow the theme templates, then you'll need to hook into the 'page_template' filter and use a template file you create in your plugin. See: https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
Depends on the situation, you can add code in your custom template, use shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields on in the Admin UI and use get_field() in your code, etc. You have lots of options in WordPress. Once you have a custom template you can just add regular PHP in there.
Yes, but you'll need to use regex and create rewrite rules using add_rewrite_rule(). Here are links that help with creating rewrite rules in WordPress
add_rewrite_rule
add_rewrite_tag
flush_rewrite_rules
WP_Rewrite API

WPBakery Post Grid Custom Query

I am using WPBakery and I would like to use the Post Grid to display the child pages of the current page. I understand that I can use a custom query within WPBakery, however, I am struggling with fetching the current post ID which is accepted by WPB.
I have a custom post type called 'partners' and some have child pages which I would like to bring through using the grid.
I'm looking to turn this into a custom query which is accepted by WPBakery.
$args = array(
'post_parent' => $post->ID,
'posts_per_page' => -1,
'post_type' => 'partners',
);
When use the Post Grid to display the child pages of the current page.
I use string:post_parent="parent page ID"&posts_per_page=-1&post_type=page

Show Multiple Post Thumbnails only in one page

I use Multiple Post Thumbnails plugin to add many thumbnails to my page.
for($i=1; $i<=10;$i++){
new MultiPostThumbnails(
array(
'label' => 'Photo - '.$i,
'id' => 'Photo-'.$i,
'post_type' => 'page'
)
);
}
But I need to have them only in one of the pages, not all. Now if I open any pages in admin, I see 10 Post Thumbnails fields. I need to specify the ID on the page and see them only in that page in admin.

Creating a delete confirmation for images using the Wordpress meta box plugin

I am using the Meta Box plugin for Wordpress. I can successfully create fields in the cms for users to upload images. I would like to extend this in two ways:
First, I would like a delete confirmation when users remove an image from the image gallery
Here is the code:
$meta_boxes[] = array(
'id' => 'project_media',
'title' => 'Project Media',
'pages' => array( 'project' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Media Gallery',
'desc' => 'Images should be sized to 983px x 661px',
'id' => $prefix . 'project_media_gallery',
'type' => 'image'
)
);
This creates upload functionality in the custom post type where users can add images to a slideshow. The problem is if the user accidentally clicks the delete button, there is no confirmation to make sure it is deleted. Can I somehow extend the plugin through functions and call an alert when this button is clicked? Something that does not involve editing the WP core?
Second, the base functionality requires the user to upload an image from their local machine. Is there a way to tap into the Media Library for this?
No idea how to even start tackling this one.
To answer the first question
First, I would like a delete confirmation when users remove an image from the image gallery
You can do that by calling a custom script file from the functions.php.
function alert_delete() {
if(is_admin()){
wp_register_script( 'alert_delete', get_bloginfo('template_url'). '/js/alert_delete.js', array('jquery'));
wp_enqueue_script('alert_delete');
}
}
and create a file named alert_delete.js in the js directory of your theme.
alert_delete.js:
// admin delete check
jQuery(document).ready(function(){
jQuery(".rwmb-delete-file").click(function() {
if (!confirm("Are you sure? This process cannot be undone.")){
return false;
}
});
});
In response to the second question...
Second, the base functionality requires the user to upload an image
from their local machine. Is there a way to tap into the Media Library
for this?
Get the latest version of the Meta Box Plugin first.
then change
'type' => 'image'
to
'type' => 'image_advanced'
which will allow you to upload from the existing Media Gallery or a new file from your computer.

Dynamically maintain virtual pages URL without create pages from admin

I created a page named UserName its URL is http://my_site/username/. On this page I am showing three links, and each link behaves as meta info for UserName.
Let's suppose:
If UserName contains info about User then the three links are:
About Me
Images
Videos
and links contains href like:
http://my_site/username/about_me
http://my_site/username/images
http://my_site/username/videos
now I create three general files like:
about_me.php
images.php
videos.php
and want to include these file by checking the URL, but I don't know how.
I did it without adding new page from wp-admin because there will be so many UserName pages but they all have same three links and will show the info about respective user.
And if I prefer to create About Me child page whose parent will be the UserName page then admin will need to create 3*(n UserName) pages where n least value is 100 and could be 1000s
But when I click any link WP says
Page Not Found
This is somewhat embarrassing, isn’t it?
I select Custom Structure from Settings and I have no more idea about WP permalinks.
You may call I need to create Virtual pages for all users.
If it is not possible then is it possible that while adding new UserName page then on published three pages (About Me, Images & Videos) will automatically with parent page newly UserName page and with a defined Page Template. If it is possible then how?
Wow I got an idea, implement & hurray it worked.
In wp-admin/includes/post.php I add my script in function edit_post( $post_data = null )
First I checked if post not already exists then run my script which is:
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $current_user_id,
'post_date' => date('Y-m-d H:i:s'),
'post_date_gmt' => date('Y-m-d H:i:s'),
'post_name' => 'Image',
'post_parent' => $post_parent,
'post_status' => 'publish',
'post_title' => 'Image',
'post_type' => 'page'
);
// Insert in to WP wp_posts table
$this_post_id = wp_insert_post( $post, $wp_error );
// Insert in to wp_postmeta table
$meta_id = update_post_meta($this_post_id , '_wp_page_template', 'page-three-columns.php');

Resources