On my blog when I add some functions for some hooks in functions.php then set feature image not work properly. When I upload the images it says
"An error occurred in the upload. Please try again later"
My added function code to functions.php file
add_action('transition_post_status', 'send_new_post', 10, 3);
// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
if('publish' === $new_status && 'publish' !== $old_status) {
$post_id = $post->ID;
echo "<script>postId = console.log(".$post_id.")</script>";
}
}
When I remove this function from functions.php file set feature image start working properly.
It's working after giving writable permission to wp-content/uploads folder
Related
If I try to use ACF inside my custom functions plugin I get this error:
Uncaught Error: Call to undefined function get_field() in
I created my custom function plugin to avoid using the default functions.php that comes with the theme, because it overrides on every update.
My code:
<?php
$author_id = get_the_author_meta('ID');
$author_badge = get_field('post_autor', 'user_'. $author_id );
?>
<img src="<?php echo $author_badge['url']; ?>" alt="<?php echo $author_badge['alt']; ?>" />
I'm using a Gantry5 theme.
I already followed the documentation and read some posts with examples, but even so I’m not being able to get it working. The field is set as User Object.
UPDATE:
I found this code in the developer's website, I'm going to be giving it a try. This code supposed to allow ACF (free version) to be loaded inside a plugin.
// Define path and URL to the ACF plugin.
define( 'MY_ACF_PATH', get_stylesheet_directory() . '/includes/acf/' );
define( 'MY_ACF_URL', get_stylesheet_directory_uri() . '/includes/acf/' );
// Include the ACF plugin.
include_once( MY_ACF_PATH . 'acf.php' );
// Customize the url setting to fix incorrect asset URLs.
add_filter('acf/settings/url', 'my_acf_settings_url');
function my_acf_settings_url( $url ) {
return MY_ACF_URL;
}
// (Optional) Hide the ACF admin menu item.
add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
function my_acf_settings_show_admin( $show_admin ) {
return false;
}
https://www.advancedcustomfields.com/resources/including-acf-within-a-plugin-or-theme/
Second Update:
Still having the same problem.
I'm trying to redirect when a file has been uploaded in media library to crop/resize it.
I'm adding a custom template and try to redirect after the admin add a media to library (admin panel).
But when I upload media I've got an error in wordpress : "An error occurred during upload. Please try again later."
And nothing comes.
My file is uploaded and saved but doesn't show in media library.
I've traveled through many topics but didn't find an answer. Maybe I can't redirect like that in a plugin.
I've got two php file in wp-contents/plugins/myplugin/plugin.php and custom-template.php
<?php
/*
Plugin name: Waouh_pictures
Description:
Version: 1.0
Author: Waouh
*/
require_once("lib/shortpixel-php-req.php");
if(!defined('ABSPATH'))
exit;
class plugin{
public function __construct(){
// Some code here
/* Add cropping template to wordpress */
function page_template( $page_template )
{
if ( is_page( 'cropping-waouh' ) ) {
$page_template = dirname( __FILE__ ) . '/crop_waouh.php';
}
return $page_template;
}
add_filter( 'page_template', 'page_template' );
/* Redirect to cropping-waouh when a file is uploaded */
function redirect_to_crop( $upload ) {
$url = get_site_url() . "/cropping-waouh";
wp_redirect( $url );
exit;
return $upload;
}
add_filter( 'wp_handle_upload', 'redirect_to_crop' );
}
}
new plugin();
?>
And my custom template :
<?php
/* Template Name: Cropping Waouh */
echo 'This is my cropping page :)';
?>
Network console log
Pretty sure I'm doing wrong but I'm new to wordpress and I'm open to any constructive comments.
If you need more informations just ask. Thank you in advance.
It seems like I can't do it that way so the thing I've done was to do it with jQuery.
I'm adding a button on my media library and hide the one who already exists.
jQuery(document).ready(function($){
$("#wp-media-grid > a").after("<form action=\"/wp-content/plugins/waouh_pictures/function.php\"><input type=\"file\" id=\"button_upload_waouh\" name=\"filename\" method=\"POST\"><input type=\"submit\" value=\"Ajouter\"></form>");
$("#wp-media-grid > a").hide();
});
And I can modify the file before uploading it into the server using croppie.
I hope I could help someone with that.
I have this plugin I've made for uploading an image before an order can be completed, but for the life of me I can't get the image to upload. $_FILES will always return nothing, I'm not sure why though. I'd like it to be that the checkout won't be completed until the image is uploaded, is this even possible?
*I've been told that woocommerce uses ajax for the cart
<?php
/*
#package UploadTest
#wordpress_plugin
Plugin Name: UploadTest
Plugin URI: null
Description:
Author: Goodship
Version: 0.0.2
Author URI: www.Goodship.co.za
*/
function add_image(){
$testLog = fopen(plugin_dir_path( __FILE__ )."testicle.txt","w") or exit ("Unable to open file!");
//if they DID upload a file...
if($_FILES['testUpload']['name']){
//if no errors...
if(!$_FILES['testUpload']['error']){
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['testUpload']['tmp_name']); //rename file
if($_FILES['testUpload']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file){
//move it to where we want it to be
move_uploaded_file($_FILES['testUpload']['tmp_name'], plugin_dir_path( __FILE__ ).'uploads/'.$new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['testUpload']['error'];
}
}
fwrite ($testLog ,$message);
fwrite ($testLog ,var_dump($_FILES));
fclose ($testLog);
}
add_action( 'woocommerce_checkout_update_order_meta', 'add_image');
function add_checkout_notice() {
echo '<input type="file" name="testUpload" />';
}
add_action( 'woocommerce_checkout_before_customer_details', 'add_checkout_notice');
?>
You need to call below function in your child theme.
function add_image($order_id){
// Do your code of upload image.
}
add_action( 'woocommerce_checkout_order_processed', 'add_image', 1, 1 );
Woocommerce checkout will always happens via Ajax ( I am not sure from what version it's been like this )
PLUGIN-DIR/woocommerce/assets/frontend/checkout.js this is the file which is responsible for the checkout action.
So uploading files from checkout page is not possible unless you intended to modify the checkout.js file by yourself.
If you still prefer uploading file from checkout page, you may refer this answer.
Have you given a look at Advanced Custom Field? https://www.advancedcustomfields.com/
You can easily achieve what you are trying to do.
Have a look at this https://www.advancedcustomfields.com/resources/acfupload_prefilter/
I'm trying to change the og:url content on specific posts but I am unsure how to implement my changes from the functions.php file.
I have tried doing this using the content I have found on the internet but believe it has been updated since then.
I have updated the class-opengraph.php file in the the wordress-seo plugin folder which works, please find my edits below:
public function url() {
$url = apply_filters('wpseo_opengraph_url',
WPSEO_Frontend::get_instance()->canonical(false));
if (is_string($url) && $url !== '' ) {
if (is_page(32721)) {
$this->og_tag('og:url', esc_url('testing'));
} else {
$this->og_tag( 'og:url', esc_url( $url ) );
}
return true;
}
return false;
}
It's not good to modify the plugin files directly because when you update the plugin, you will lose all your changes to those files.
There are two solutions that I have found to do such thing.
You get the instance of the class WPSEO_Frontend, then update it's options for the og:url.
e.g.
$object = WPSEO_Frontend::get_instance();
$object->options['og_url'] = esc_url( $url );
This can be added before the wp_head()
You can use add_filter to hook a function to the filter action. We use the filter action below.
Filter: 'wpseo_opengraph_url' - Allow changing the OpenGraph URL
e.g.
function update_og_url($url) {
return "http://www.yoursampleurl.com";
}
add_filter('wpseo_opengraph_url', 'update_og_url', 10, 1);
Source: Wordpress SEO API
I am searching a solution to add a custom template for plugin on short code. But I am unable to do it successfully.
I have made a template folder in my plugins folder and put a custom template in it. I want to show this template by putting a short code. For this I have written following piece of code.
function wp_parse_login()
{
add_action('template_redirect', 'my_template');
function my_template()
{
include ('template/login.php');
exit;
}
}
add_shortcode('parse_login_page','wp_parse_login');
but it's not working. I have include this file to my main plugin file. I think I am leaving some hooks.
Here an adapted example from WordPress Codex
function wp_parse_login() {
ob_start();
include ('template/login.php');
return ob_get_clean();
}
add_shortcode('parse_login_page','wp_parse_login');
You first set a page template in DB Like:
$table_post_meta = $wpdb->prefix.'postmeta';
$meta_data = array(
'post_id' => $post_id, (Get dynamically post id of a page)
'meta_key' => '_wp_page_template',
'meta_value' => 'template/login.php' (Give the file path)
);
$wpdb->insert($table_post_meta,$meta_data) or die(mysql_error());
The you write in your this code in your template page:
function wp_parse_login() {
ob_start();
include ('template/login.php');
return ob_get_clean();
}
add_shortcode('parse_login_page','wp_parse_login');
Hope you find your solution.