How to add multiple featured images without a plugin? - php

This other Stack Overflow question is exactly what I need, but the answer on it is a link that is 5+ years outdated.
It tells me to change the following code:
public function enqueue_admin_scripts() {
wp_enqueue_script("featured-image-custom", plugins_url(basename(dirname(__FILE__)) . '/js/multi-post-thumbnails-admin.js'), array('jquery'));
}
to this:
public function enqueue_admin_scripts() {
$template_url = get_bloginfo('template_url') . '/js/multi-post-thumbnails-admin.js';
wp_enqueue_script("featured-image-custom", $template_url, array('jquery'));
}
but the updated plugin now has this instead:
public function enqueue_admin_scripts( $hook ) {
global $wp_version, $post_ID;
// only load on select pages
if ( ! in_array( $hook, array( 'post-new.php', 'post.php', 'media-upload-popup' ) ) )
return;
if (version_compare($wp_version, '3.5', '<')) {
add_thickbox();
wp_enqueue_script( "mpt-featured-image", $this->plugins_url( 'js/multi-post-thumbnails-admin.js', __FILE__ ), array( 'jquery', 'media-upload' ) );
} else { // 3.5+ media modal
wp_enqueue_media( array( 'post' => ( $post_ID ? $post_ID : null ) ) );
wp_enqueue_script( "mpt-featured-image", $this->plugins_url( 'js/multi-post-thumbnails-admin.js', __FILE__ ), array( 'jquery', 'set-post-thumbnail' ) );
wp_enqueue_script( "mpt-featured-image-modal", $this->plugins_url( 'js/media-modal.js', __FILE__ ), array( 'jquery', 'media-models' ) );
}
wp_enqueue_style( "mpt-admin-css", $this->plugins_url( 'css/multi-post-thumbnails-admin.css', __FILE__ ) );
}
I've tried leaving it as is, changing all of them like the blog link says, and adding the $template_url variable every time is says $this. In the back end of WordPress, I see the 2nd featured image, but the only thing it does is add a number sign (#) to the editor's url.
Here's also my code added to the functions.php file:
// Load external file to add support for MultiPostThumbnails. Allows you to set more than one "feature image" per post.
require_once('library/multi-post-thumbnails.php');
// Define additional "post thumbnails". Relies on MultiPostThumbnails to work
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => '2nd Feature Image',
'id' => 'feature-image-2',
'post_type' => 'page'
)
);
new MultiPostThumbnails(array(
'label' => '3rd Feature Image',
'id' => 'feature-image-3',
'post_type' => 'home'
)
);
};
Please and thank you for your time.

Related

Wordpress, redirect user after clicking delete post shortcut button

I am working on a new plugin. I am dealing with a problem that I have outlined in the title. My intention is to redirect the user to the My Pages page after the user clicks the "Delete page" button.
Here is my code:
function custom_admin_bar_delete_link( $wp_admin_bar ) {
global $post;
if( is_admin() || ! is_object( $post ) )
return;
if ( ! current_user_can( 'delete_pages' ) )
return;
if ( $post->post_type != 'page' )
return;
$args = array(
'id' => 'delete_link',
'title' => 'Delete this page',
'href' => get_delete_post_link( $post->ID ),
'meta' => array( 'class' => 'delete-link' )
);
$wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'custom_admin_bar_delete_link', 999 );
function custom_page_delete_redirect( $location, $post_id ) {
$post = get_post( $post_id );
if ( 'page' === $post->post_type && 'trash' === get_post_status( $post_id ) ) {
return admin_url( 'edit.php?post_type=page' );
}
return $location;
}
add_filter( 'wp_redirect', 'custom_page_delete_redirect', 10, 2 );
Thank you.
Without thinking too hard about it, I would add an ID to your button and just write some jQuery to do the redirect.
(The PHP: NOT tested)
EDIT:
Per the Wordpress documentation, you should be able to add an ID:
https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/
$args = array(
'id' => 'delete_link',
'title' => 'Delete this page',
'href' => get_delete_post_link( $post->ID ),
'id' => "someid",
'meta' => array( 'class' => 'delete-link' )
);
The jQuery:
EDIT:
How are you adding the jQuery? You should probably save it in a separate file in your plugin folder (maybe in a subfolder named "js") and enqueue it in your plugin. The 2nd answer to this question would be a good place to start:
https://wordpress.stackexchange.com/questions/42641/how-to-include-a-simple-jquery-file-into-a-wordpress-plugin
jQuery(document).ready(function($){
$('body').on('click','#someid',function(event){
// window.alert('jquery executing');
setTimeout(function(){window.location.href = "http://example.com/page/";}, 500);
});
});
Thoughts: I added a timeout, because Wordpress loads slowly sometimes, and I've had to do this for other applications in Wordpress, but try without and see if it performs without the timeout.
Have a look at this answer:
https://wordpress.stackexchange.com/questions/132196/get-delete-post-link-redirect
The question is similar, and this may get you where you need to go.

WordPress Plugin to create a React Public page on activation

I'm building a WordPress plugin that creates an admin menu and stores the value in the WordPress backend and then shows this stored value on a Public page. But the code is not creating a Public page where I want to load a custom react page.
In the root directory, I have wp-react-kickoff.php file like this
<?php
/**
* Plugin Name: Batmobile Design
* Author: Batman
* Author URI:
* Version: 1.0.0
* Description: WordPress React KickOff.
* Text-Domain: wp-react-kickoff
*/
if( ! defined( 'ABSPATH' ) ) : exit(); endif; // No direct access allowed.
/**
* Define Plugins Contants
*/
define ( 'WPRK_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
define ( 'WPRK_URL', trailingslashit( plugins_url( '/', __FILE__ ) ) );
/**
* Loading Necessary Scripts
*/
add_action( 'admin_enqueue_scripts', 'sd_scripts' );
function sd_scripts() {
wp_enqueue_script( 'wp-react-kickoff', WPRK_URL . 'dist/bundle.js', [ 'jquery', 'wp-element' ], wp_rand(), true );
wp_localize_script( 'wp-react-kickoff', 'appLocalizer', [
'apiUrl' => home_url( '/wp-json' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
] );
}
require_once WPRK_PATH . 'classes/class-create-admin-menu.php';
require_once WPRK_PATH . 'classes/class-create-settings-routes.php';
require_once WPRK_PATH . 'classes/class-public-page.php';
Then I have created a folder called classes where I have
class-create-admin-menu.php,
class-create-settings-routes.php,
class-public-page.php,
wprk-public-page-template.php.
The code specific to create Public page looks like this
<?php
/**
* This file will create Public Page
*/
function wprk_create_public_page() {
$page = [
'post_type' => 'Automatic page',
'post_title' => 'WP React KickOff Public Page',
'post_content' => 'asasasas',
'post_status' => 'publish',
'post_author' => 1,
];
$page_id = wp_insert_post( $page );
update_post_meta( $page_id, '_wp_page_template', 'wprk-public-page-template.php' );
}
register_activation_hook( __FILE__, 'wprk_create_public_page' );
And my wprk-public-page-template.php
<div id="wprk-public-app"></div>
<?php
wp_enqueue_script( 'wp-react-Kickoff', WPRK_URL . 'dist/bundle.js', [], wp_rand(), true );
wp_localize_script( 'wp-react-Kickoff', 'appLocalizer', [
'apiUrl' => home_url( '/wp-json' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
] );
And In the ReactFolder, I have App.js, both Settings (This is for admin menu, it is working properly) and Public Page rendered like this
import React from 'react';
import PublicPage from './components/PublicPage';
import Settings from './components/Settings';
function App() {
return(
<React.Fragment>
<Settings />
<PublicPage />
</React.Fragment>
)
}
export default App;
And for testing, let the Public page, look like this
import axios from 'axios';
const apiUrl = appLocalizer.apiUrl;
const nonce = appLocalizer.nonce;
import React from 'react';
function PublicPage(props) {
return (
<div>
<h1>hello world asasa</h1>
</div>
);
}
export default PublicPage;
I'm quite new to programming. Could someone help me identify why the Public page is not getting created?
Please let me know if you need additional information to troubleshoot?
your post_type argument in your create page function isnt valid.
If you have a custom post type you'll want to use the slug, otherwise, in your case if i understand correctly you want to create a standard wordpress page which the post type is page.
function wprk_create_public_page() {
$page = [
'post_type' => 'page',
'post_title' => 'WP React KickOff Public Page',
'post_content' => 'asasasas',
'post_status' => 'publish',
'post_author' => 1,
];
$page_id = wp_insert_post( $page );
update_post_meta( $page_id, '_wp_page_template', 'wprk-public-page-template.php' );
}
register_activation_hook( __FILE__, 'wprk_create_public_page' );

Wordpress Nav Tab Items Add Filter function to return array with new tab

I have a settings page in my plugin which has multiple tabs generated like so:
private function get_settings_nav_tab_items() {
$item_features = array(
'tab' => 'features',
'caption' => __( 'Features', 'textdomain' ),
'content' => plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/settings-tab-features.php'
);
$item_setup = array(
'tab' => 'setup',
'caption' => __( 'Setup', 'textdomain' ),
'content' => plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/settings-tab-setup.php'
);
$nav_tab_items = array();
array_push( $nav_tab_items, $item_features );
array_push( $nav_tab_items, $item_setup );
//filter to hook new tabs for extra settings
return apply_filters( 'settings_nav_tab_items', $nav_tab_items );
}
in another module when the module is activated i am trying to hook into the tabs and add a new settings tab for the module when activated.
I am calling this filter function:
add_filter( 'settings_nav_tab_items', 'add_announcement_settings_tab' );
The callback for this is this function:
function add_announcement_settings_tab( $nav_tab_items ) {
$item_announcements = array(
'tab' => 'announcements',
'caption' => __( 'Announcments', 'textdomain' ),
'content' => plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/settings-tab-announcments.php'
);
return $nav_tab_items;
}
No extra tab is appearing when this is called. Am I completely off track here??
I figured this out.
The reason it did not work is stupidly simple I am so annoyed!! lol
Becasue I am in OOP (object orientated) I had to add my callback into an array.
So from this:
add_filter( 'settings_nav_tab_items', 'add_announcement_settings_tab' );
To this:
add_filter( 'settings_nav_tab_items', array($this, 'add_announcement_settings_tab') );

functions.php with wp_redirect($url); exit(); makes wordpress site blank

I'm creating a form for the users to submit posts from the front end.
Once the form is submitted the users should be redirected to the post they have just created.
I have this code in my functions.php. However it makes my website blank...
I think it's related to the exit() line, I've tried to modify it but it doesn't work, nothing happens at all. It just shows a white page.
<?php
wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
wp_enqueue_script( 'validation' );
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish'
);
$post_id = wp_insert_post($post_information);
$url = get_permalink( $post_id );
wp_redirect($url);
exit();
?>
Do you have any ideas? How can I fix that? Thanks!
OK, it won't work like that.
First of all, you shouldn't add scripts like that when the functions.php is loaded (because it's loaded much too early, before WP has actually decided what to do with the request that comes from the browser) - use the wp_enqueue_scripts for that:
<?php
function add_my_scripts() {
wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
wp_enqueue_script( 'validation' );
}
add_action( 'wp_enqueue_scripts', "add_my_scripts");
?>
Your creation of a new post runs on every request - even for that request when your browser wants to show that new post.
Depending on what exactly you need, you might want to put this into an action hook as well, but it should help already if you check that it was actually a POST request that contains a postTitle, something like this:
<?php
if( $_SERVER["REQUEST_METHOD"] == "POST" && array_key_exists("postTitle", $_POST)) {
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish'
);
$post_id = wp_insert_post($post_information);
if(is_wp_error($post_id)) {
print "An error occured :(\n";
var_export($post_id);
}
else {
$url = get_permalink( $post_id );
wp_redirect($url);
}
exit();
}
?>

Override woocommerce class files

I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-frontend-scripts.php in woocommerce/includes/
The function I'm looking to modify is:
private static function get_script_data( $handle ) {
global $wp;
switch ( $handle ) {
case 'wc-single-product' :
return array(
'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options', array(
'rtl' => is_rtl(),
'animation' => 'slide',
'smoothHeight' => true,
'directionNav' => false,
'controlNav' => 'thumbnails',
'slideshow' => false,
'animationSpeed' => 500,
'animationLoop' => false, // Breaks photoswipe pagination if true.
) ),
'zoom_enabled' => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ),
'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ),
'photoswipe_options' => apply_filters( 'woocommerce_single_product_photoswipe_options', array(
'shareEl' => false,
'closeOnScroll' => false,
'history' => false,
'hideAnimationDuration' => 0,
'showAnimationDuration' => 0
) ),
'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ),
);
break;
}
return false;
}
For my product slider, I need to show prev and next arrows on single product page. So I need to change 'directionNav' as true.
How do I do this without making changes to the core files?
Have you created a child theme yet? If not, that's your first step. Here's a link from the WordPress codex:
https://codex.wordpress.org/Child_Themes
Then instead of editing that core file, you hook into that function instead, and write that code inside of the functions.php file you created in your child theme. If you want to add code to the existing function, you'll want to use an action hook. If you want to modify the code, you use a filter. It looks like you're trying to change the code, so probably a filter is best.
Here's how it would look using a filter:
add_filter( 'get_script_data', 'change_nav_direction' );
function change_nav_direction( $variable ) {
//any other code you may need
'directionNav' => true,
//you must have a return
return $variable;
}
Here's a link to an article from WooCommerce that might help:
https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/

Categories