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.
Related
I have written a module into my WordPress theme, its overly complicated and irrelevant.
I need to include a block of code and want a reliable way to determine if the user is currently editing specific pages.
Essentially, I want the equivalent of the front-end check:
if (is_front_page()) {
But, for when on the editor pages.
It is required to be able to target pages that are not given special names like front-page / shop-page too. So I may need to target by post ID.
What is the cleanest way to perform this check?
I have tried the function above, along with:
$screen = get_current_screen();
However, this forces a critical error:
Fatal error: Uncaught Error: Call to undefined function get_current_screen() in ... points to line.
A couple other methods have also been attempted, I just note a few. I thought I would be able to get one to work but none seem to do the trick. Please can someone point me in the right direction?
EDIT
Adding example snippet
// This creates the fatal error.
// $screen = get_current_screen();
// var_dump($screen);
// This is for the front-end
// if (is_front_page()) {
// global $pagenow;
// var_dump($pagenow);
// if ($pagenow == 'post.php') {
// This returns string(8) "post.php" which is not descript enough.
// This is front-end
// global $post;
// $post_id = $post->ID;
// Post ID 3887 is Home
if ($post_id == '3887') {
echo 'hello!';
require_once('inc/overlays/page-template/fc-front-page.php');
} else {
echo 'Well that did not work!';
}
The reason I am doing this is irrelevant, I simply want to include a file in a different folder, the file called will be different depending on which page is currently being edited!
get_current_screen() is only valid on admin pages.
The WP_Screen object is set after the admin_init action hook via the current_screen action hook. You can refer to the Plugin API/Action Reference and the Actions Run During a Typical Request for the firing hook sequence.
To make sure the WP_Screen object is firing, you can use the following function. It will output the screen object as a html commentary on admin pages that support it.
add_action( 'current_screen', 'print_r_get_current_screen' );
if ( ! function_exists( 'print_r_get_current_screen' ) ) {
function print_r_get_current_screen() {
echo PHP_EOL . '<!--' . PHP_EOL;
print_r( get_current_screen() );
echo PHP_EOL . '-->' . PHP_EOL;
};
};
But I don't think using the current screen will help you in achieving what you're trying to do.
You can use get_option( 'page_on_front' ); to get the id of the front page.
add_action( 'init', 'print_r_get_option_page_on_front' );
if ( ! function_exists( 'print_r_get_option_page_on_front' ) ) {
function print_r_get_option_page_on_front() {
echo PHP_EOL . '<!--' . PHP_EOL;
print_r( get_option( 'page_on_front' ) ); //... 0 if undefined, page ID if defined
echo PHP_EOL . '-->' . PHP_EOL;
};
};
For woocommerce I'm pretty sure you can use woocommerce_get_page_id( 'shop' ).
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.
Woocommerce took out the option for enabling a lightbox feature for your image gallery earlier this year. They have in their documentation to add code if you want to enable the gallery features but don’t actually say where.
https://woocommerce.wordpress.com/2017/02/28/adding-support-for-woocommerce-2-7s-new-gallery-feature-to-your-theme/
This is a significant frontend change that can be broken down in to three separate new features;
• Image zoom / magnification
• Lightbox
• Slider
To enable each of these features in your theme you must declare support using add_theme_support() like so:
add_action( 'after_setup_theme', 'yourtheme_setup' );
function yourtheme_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
This allows you the flexibility to pick and choose exactly which features you want to include/exclude in your theme or at your store.
I am not a developer, I don’t have a developer (and shame on WC for not making this an option that end users can opt for or not without having to add code!)
I need to know where to put this code. I am using a child theme called mystile1. I have files called “Theme Functions (function.php)” and one called “custom.css” that’s says it is specifically for adding code to modify my child theme styles.
I don’t know which file I should put the above coding in and where. Nowhere does each of these files have a line called “after_setup_theme” So would I be safe in just adding the code as follows in one of those files (which one?) replacing “yourtheme” with the name of my theme:
add_action( 'after_setup_theme', 'mystile1_setup' );
function mystile1_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
Or any other suggestions are greatly appreciated.
Thank you.
IN RESPONSE:
Below is what is in my functions.php file. would I put the code at the top in between new brackets or down in the section that says:
/-----------------------------------------------------------------------------------/
/* You can add custom functions below /
/-----------------------------------------------------------------------------------*/
function mystile1_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
?>
"MY FUNCTIONS.PHP FILE" INCLUDES
<?php
// File Security Check
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'You do not have sufficient permissions to access this page!' );
}
?>
<?php
/-----------------------------------------------------------------------------------/
/* Start WooThemes Functions - Please refrain from editing this section /
/-----------------------------------------------------------------------------------*/
// Define the theme-specific key to be sent to PressTrends.
define( 'WOO_PRESSTRENDS_THEMEKEY', 'zdmv5lp26tfbp7jcwiw51ix9sj389e712' );
// WooFramework init
require_once ( get_template_directory() . '/functions/admin-init.php' );
/-----------------------------------------------------------------------------------/
/* Load the theme-specific files, with support for overriding via a child theme.
/-----------------------------------------------------------------------------------/
$includes = array(
'includes/theme-options.php', // Options panel settings and custom settings
'includes/theme-functions.php', // Custom theme functions
'includes/theme-actions.php', // Theme actions & user defined hooks
'includes/theme-comments.php', // Custom comments/pingback loop
'includes/theme-js.php', // Load JavaScript via wp_enqueue_script
'includes/sidebar-init.php', // Initialize widgetized areas
'includes/theme-widgets.php', // Theme widgets
'includes/theme-install.php', // Theme installation
'includes/theme-woocommerce.php' // WooCommerce options
);
// Allow child themes/plugins to add widgets to be loaded.
$includes = apply_filters( 'woo_includes', $includes );
foreach ( $includes as $i ) {
locate_template( $i, true );
}
/-----------------------------------------------------------------------------------/
/* You can add custom functions below /
/-----------------------------------------------------------------------------------*/
// CUSTOM FUNCTION ADDED TO ADDRESS LACK OF ADD-TO-CART BUTTONS ON VARIABLE ITEMS
// AS DOCUMENTED AT: http://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-checkout-button-not-showing-on-woo-commerce-product/page/2?replies=36#post-3263097
function mv_my_theme_scripts()
{
wp_enqueue_script('add-to-cart-variation', get_template_directory_uri() . '/js/add-to-cart-variation.js',array('jquery'),'1.0',true);
}
add_action('wp_enqueue_scripts','mv_my_theme_scripts');
/-----------------------------------------------------------------------------------/
/* Don't add any code below here or the sky will fall down /
/-----------------------------------------------------------------------------------*/
?>`
You have to put the code in your function.php between <?php and ?> tags.
As additional note: you can put all of these gallery' effects or only few of them on your site. For example if performance of your site is degrading you can delete or put // to
add_theme_support( 'wc-product-gallery-zoom' );
or to other effects.
Suppose I have a clean wordpress install, with a basic custom theme.
In that theme, I have a custom page template which is just an iframe, which is pointed at a webapp on a different domain.
So suppose my wordpress install can be reached at http://example.com, and my page with the iframe template is located at http://example.com/members/.
I now want to add dynamic routes, so that all requests to http://example.com/members/login, or http://example.com/members/event/1 (for example) all go to http://example.com/members/ but pass the second part of the route ('/login', or '/event/1') to the iframe inside.
What would be the best way to accomplish this, without having to hack into Wordpress' internals?
I found this plugin: https://wordpress.org/plugins/wp-on-routes/ but much to my dismay I discovered that when I tried using it it completely overwrites Wordpress' built in routing, which meant I would have to manually re-add each and every URL (as I understand it, I'm not that accomplished in PHP), which is a no go as my client still needs to be able to post without manually editing php files.
Thank you for reading.
You can add routing using the add_rewrite_rule hook like so:
function custom_rewrite_rule() {
add_rewrite_rule('members/([^/]+)/([^/]+)/?$',
'index.php?memberspage=$matches[1]&event_id=$matches[2]',
'top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
You may need to create several depending on the URLs you rewriting. You can then use the URL parameters in your template to load the appropriate page in your iframe.
I managed to find a solution for my problem, thanks to Fencer04's suggestion. I found this page: https://developer.wordpress.org/reference/functions/add_rewrite_rule/ where I found an example that was close enough to my problem to work.
So in functions.php:
function custom_rewrite_rule(){
$page_id = 318; // replace this ID with the page with the iFrame template
$page_data = get_post($page_id);
if(!is_object($page_data)){
return; // all other pages don't have to support custom deeplinks
}
// catches deeplinks 1 level deep, i.e.: /members/profile
add_rewrite_rule(
$page_data->post_name . '/([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]',
'top'
);
// catches deeplinks 2 levels deep, i.e.: /members/profile/edit
add_rewrite_rule(
$page_data->post_name . '/([^/]+)/([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]&members_param=$matches[2]',
'top'
);
// catches 3 levels deep, i.e. /members/profile/edit/confirm
add_rewrite_rule(
$page_data->post_name . '/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]&members_param=$matches[2]&members_param2=$matches[3]',
'top'
);
}
add_action('init', custom_rewrite_rule);
Next I added filters for the new query_vars:
add_filter('query_vars', function($vars) {
$vars[] = "memberspage";
$vars[] = "members_param";
$vars[] = "members_param2";
return $vars;
});
And then in my template-iframe.php, I can access these parameters like so:
<?php
// get query strings
global $wp_query;
$page = $wp_query->query_vars['memberspage'];
$params = $wp_query->query_vars['members_param'];
$params2 = $wp_query->query_vars['members_param2'];
$membersBaseURL = 'http://members.domain.com/';
$iframeURL = $membersBaseURL;
if(isset($page)){
$iframeURL = $iframeURL . $page . '/';
}
if(isset($params)){
$iframeURL = $iframeURL . $params . '/';
}
if(isset($params2)){
$iframeURL = $iframeURL . $params2 . '/';
}
?>
<iframe id="iframeLeden" src="<?php echo($iframeURL) ?>" frameborder="0"></iframe>
So now if I go to http://www.domain.com/members/login, it'll show me the correct static WP page, with inside an iframe that shows the page http://members.domain.com/login/ .
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