i need a trick about wordpress.
i want replace a word from my displaying post title at whole website.(not permantly or not on database. only show temporary at browser.)
for example: i want change post word to text
here is list my post titles:
1- This is first post title
2- This is second post title
3- This is third post title
4- This is fourth post title
here is my new post title list:
1- This is first text title
2- This is second text title
3- This is third text title
4- This is fourth text title
How can i do this?
in your functions.php:
function title_changer($title) {
global $post;
$title = str_replace('text','post',$post->post_title);
return $title;
}
add_action('the_title','title_changer');
go to your single.php in your theme path
find get_the_title or the_title, for each one you may change the title_changer function with the specific functions used in the single.php
you can do using Jquery, but you've to change few things as according to your HTML tags
h2.entry-title HERE you've to change your post title CSS class and I'm hoping there is an anchor tag inside heading, so I'm replacing the HTML of anchor tag, Please change accordingly and add in your functions.php file.
add_action( 'wp_footer', 'replace_title' );
function replace_title() { ?>
<script>
(function($) {
jQuery('h2.entry-title').each(function(index, value) {
let text = jQuery(this).find("a").html();
let result = text.replace("post", "W3Schools");
jQuery(this).find("a").html(result);
});
})(jQuery);
</script>
<?php
}
This is my approach I don't know how good it is but it works.
function menuarray() {
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ "primary" ] ) && !is_admin() ) {
$menu = wp_get_nav_menu_object($locations[ "primary" ]);
$menuitems = wp_get_nav_menu_items( $menu->term_id );
$idCats = array_column($menuitems, 'title');
return $idCats;
}
}
global $menutem;
$menutem = menuarray();
First I create this function globally in functions.php to I don't repeat it inside the function.
After that I create
add_filter('the_title', 'remove_header_metis', 10,2);
function remove_header_metis($title, $id) {
global $menutem;
if ( !is_admin() && ('post' == get_post_type($id) || 'page' == get_post_type($id) ) && !in_array($title, $menutem) ) {
$title = "";
}
return $title;
}
In this way, my menu stays ok and I change what I needed. You can tweak this for your needs.
I have a beginner question: I want to add a product search form to a WordPress theme via a hook. I want to wrap the product search in a div in order to be able to style it. How can I add the div to the following:
add_action('after_main', my_custom_function');
my_custom_funtion() {
get_product_search_form;
}
Thanks for your help.
It's pretty simple:
add_action('after_main', 'my_custom_function');
function my_custom_function() {
echo '<div class="product-search">' . get_product_search_form( FALSE ) . '</div>';
}
Then, anywhere there is a do_action( 'after_main' ) in your code, this will echo out the search form wrapped in the div. You can change the class, I just added that for the example.
Edit: passed FALSE to the function. get_product_search_form() echoes by default. Passing FALSE returns it instead.
Second edit: You can also use the filter get_product_search_form like this:
add_filter( 'get_product_search_form', function( $form ) {
return '<div class="product-search">' . $form . '</div>';
});
I have created a repeatable field with CMB2, and created a normal field. This is the function of https://pastebin.com/XUQgkvbi
If you use foreach for repeatable using a post or page then you can show data as: https://pastebin.com/C35vWGDs
And Call normal field without repeatable, then
<?php $ entries = get_post_meta (get_the_ID (), 'yourprefix_group_demo', true); ?>
<?php echo $ entries; ?>
also work.
But the problem is, I do not want to use the above function on any page or post. I want to use it in the Options Page. The above Function option has been added to the option page, but I can not do the data show of those files in any way.
I've tried get_post_meta () and get_option () with two functions, but in no way can I show data from the option page. How can I get the data from the above fields (option page) to the show in the frontend? Please help with a little bit.
I got solution, The options are stored in a single option field. You would loop through the news-section groups with something like this:
$settings = get_option( 'repeatable-news-options.php', array() );
if ( ! empty( $settings['news-section'] ) ) {
foreach ( $settings['news-section'] as $section ) {
echo $section['title'] . '<br/>';
}
}
that link https://wordpress.org/support/topic/how-to-display-data-from-cmb2-option-page/
problem solved.
I've used ACF's "Flexible Content" to create a fairly advanced "page builder" where authors can create a "section" (basically just a wrapping element with a CSS class name and ID) and then add all my flexible content (images, wysiwyg etc) to it.
What I'd like to do is hide some fields for non admins. I don't want any old editor to be able to go in and change a section's ID or class names (as it would mess up the layout).
I'm aware of the "Rules" panel in the ACF admin where you can choose to only display a certain field group to one type of user role, but what I want is that same thing but for individual fields.
It doesn't appear to be doable from the admin interface, but I'm wondering if someone knows how it could be done from my functions.php file? Perhaps some filter or action I can hook into and disable certain fields based on the current user's role?
I've attached two screenshots showing what I'd like hidden:
I'd like to hide these choices from the "Add row" menu:
And I'd like these panels to be invisible to non admins:
Edit: While we're at it, I wouldn't mind hiding individual fields from a repeatable too. You'll notice a "Modifiers" field in the first screenshot, that too would be nice to hide from non admins. I guess the solution would be pretty much the same for both problems?
As of ACF 5.0.0 there is an easier way to do this without having to disable the field or output CSS. If you use the acf/prepare_field hook and return false the field will not render.
<?php
function so37111468_hide_field( $field ) {
// hide the field if the current user is not able to save options within the admin
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
return $field;
}
add_filter( 'acf/prepare_field/key=MYFIELDKEY', 'so37111468_hide_field' );
?>
The documentation for that filter can be found here: https://www.advancedcustomfields.com/resources/acf-prepare_field/
I haven't managed to actually hide the fields, but I have managed to disable them. Unfortunately simply setting them to disabled in the acf/load_field action wasn't enough to remove them from the dropdown menu so I also added some CSS to the admin page to visually hide them at least. This is good enough seeing as the editors of the site won't exactly do their best to break it.
<?php
/**
* Hide some "ACF Section" related custom fields
*/
add_action('acf/load_field', 'sleek_hide_acf_section_fields', 10, 1);
function sleek_hide_acf_section_fields ($field) {
$hide = array('section_name', 'section_modifiers', 'modifiers');
global $current_user;
if ((isset($field['_name']) and in_array($field['_name'], $hide)) and (is_admin() && is_user_logged_in() && !in_array('administrator', $current_user->roles))) {
$field['disabled'] = true;
}
return $field;
}
add_action('admin_head', 'sleek_hide_acf_section_fields_css');
function sleek_hide_acf_section_fields_css () {
$hide = array('section_name', 'section_modifiers', 'modifiers');
global $current_user;
if (is_admin() && is_user_logged_in() && !in_array('administrator', $current_user->roles)) {
echo '<style>';
foreach ($hide as $h) {
echo 'div.acf-fc-popup a[data-layout="' . $h . '"]{display: none}';
}
echo '</style>';
}
}
have an interesting conundrum. I need to load about 8 javascript files and the same number of styles for my plugin. These are only needed where ever my shortcode is ran.
I've tried to load them with print_styles and print_scripts but they aren't rendering properly, plus to do so breaks xhtml validation. So at the moment they load on every page and due to the number of files needed its not feasible to leave it like this.
On another project I wrote a function into my plugin's index.php file that would take the current page, search it for my shortcode and if found only then would it print the scripts, but this is an ugly hack.
Has anybody got any suggestions or solutions?
any help would be appreciated,
regards,
Daithi
to answer my own question... I had it write the first time. You have to search each page to check that your shortcode is being used. This has to be done when page data is loaded and before page is displayed. To me it is complete overkill on the system, but unfortunately it is the way it is. I got this information from:
get_shortcode_regex
and
old nabble
So first:
add_action('template_redirect','wp_my_shortcode_head');
then:
function wp_my_shortcode_head(){
global $posts;
$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches);
if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') {
//shortcode is being used
}
}
replace 'YOURSHORTCODE' with the name of your shortcode and add your wp_enqueue_scripts into where it says //shortcode is being used.
I read a solution in here: http://scribu.net/wordpress/conditional-script-loading-revisited.html
Basically if using wordpress 3.3 you can enqueue your scripts in your short code function.
function my_shortcode($atts){
wp_enqueue_script( 'my-script', plugins_url( 'plugin_name/js/script.js' ), array('jquery'), NULL, true);
// if you add a css it will be added to the footer
//wp_enqueue_style( 'my-css', plugins_url( 'plugin_name/css/style.css' ) );
//the rest of shortcode functionality
}
Loading Scripts and Styles Dynamically Per Page Using a Shortcode
Advantages
Does not search through all the posts everytime the shortcode is called.
Able to add styles as well as scripts dynamically only when shortcode is on the page.
Does not use regexes since they tend to be slower than strstr() or strpos(). If you need to pickup args then you should use the shortcode regex mentioned above.
Reduces file calls
Explanation of Code
Finds the shortcodes on page using the save_post hook only when the post is not a revision and matches the specified post_type.
Saves the found post ids as an array using add_option() with autoload set to yes unless the entry is already present. Then it will use update_option().
Uses hook wp_enqueue_scripts to call our add_scripts_and_styles() function.
That function then calls get_option() to retrieve our array of page ids. If the current $page_id is in the $option_id_array then it adds the scripts and styles.
Please note: I converted the code from OOP Namespaced classes so I may have missed something. Let me know in the comments if I did.
Code Example: Finding Shortcode Occurences
function find_shortcode_occurences($shortcode, $post_type = 'page')
{
$found_ids = array();
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
);
$query_result = new WP_Query($args);
foreach ($query_result->posts as $post) {
if (false !== strpos($post->post_content, $shortcode)) {
$found_ids[] = $post->ID;
}
}
return $found_ids;
}
function save_option_shortcode_post_id_array( $post_id )
{
if ( wp_is_post_revision( $post_id ) OR 'page' != get_post_type( $post_id )) {
return;
}
$option_name = 'yourprefix-yourshortcode';
$id_array = find_shortcode_occurences($option_name);
$autoload = 'yes';
if (false == add_option($option_name, $id_array, '', $autoload)) update_option($option_name, $id_array);
}
add_action('save_post', 'save_option_shortcode_id_array' );
Code Example: Shortcode Dynamically Include Scripts and Styles
function yourshortcode_add_scripts_and_styles() {
$page_id = get_the_ID();
$option_id_array = get_option('yourprefix-yourshortcode');
if (in_array($page_id, $option_id_array)) {
wp_enqueue_script( $handle, $src, $deps, $ver, $footer = true );
wp_enqueue_style( $handle, $src , $deps);
}
}
add_action('wp_enqueue_scripts', 'yourshortcode_add_scripts_and_styles');
Just read this tutorial over here: http://scribu.net/wordpress/optimal-script-loading.html
Seems to be the best way.
add_action('init', 'register_my_script');
add_action('wp_footer', 'print_my_script');
function register_my_script() {
wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
}
function print_my_script() {
global $add_my_script;
if ( ! $add_my_script )
return;
wp_print_scripts('my-script');
}
In this case, the script will be enqueued only if the $add_my_script
global was set at some point during the rendering of the page.
add_shortcode('myshortcode', 'my_shortcode_handler');
function my_shortcode_handler($atts) {
global $add_my_script;
$add_my_script = true;
// actual shortcode handling here
}
So, the script will be added if [myshortcode ...] was found in any of
the posts on the current page.
Load Scripts and Styles if Post/Page has Short Code
The best solution is to load the files into the page header if, and only if, the current post or page has the short code inside its content. And that’s exactly what the following function does:
function flip_register_frontend_assets()
{
//register your scripts and styles here
wp_register_style('pp_font','plugin_styles.css', null, null, 'all');
global $post;
//check whether your content has shortcode
if(isset($post->post_content) && has_shortcode( $post->post_content, 'your-
shortcode')){
//Enqueue your scripts and styles here
wp_enqueue_style( 'pp_font');
}
}
Simply place this function inside of one of your plugin files and you’re good to go.
You will need to replace [your-shortcode] with the short code you want to search for, and you will also need to replace plugin_styles.css with your stylesheet name.
You can just use this code to check if the shortcode is implemented in page content or in sidebar widgets.
<?php
if ( shortcode_exists( 'gallery' ) ) {
// The [gallery] short code exists.
}
?>
I use WordPress Version 5.4 with OOP style of code i dont know if this affect why none of the above solutions didn't work for me so i come up with this solution:
public function enqueue_scripts() {
global $post;
//error_log( print_r( $post, true ) );
//error_log( print_r( $post->post_content, true ) );
//error_log( print_r( strpos($post->post_content, '[YOUR_SHORTCODE]'),true));
if ( is_a( $post, 'WP_Post' ) && strpos($post->post_content, '[YOUR_SHORTCODE]') )
{
wp_register_style('my-css', $_css_url);
wp_register_script('my-js', $_js_url);
}
}
Hope this help someone.
How many pages are these scripts going to be loaded on? Would it be feasible to maintain an array of pages, and only load the scripts/stylesheets when the current page is in the array?
Otherwise, without scanning the code there is no way to do this, as WP doesn't even know the shortcode exists until well into the page load.
BraedenP is right, I'm pretty sure there is no way to detect shortcode usage at the execution time of wp_enqueue_scripts / when the stylesheets load.
Is there any reason you must do this in 8 files? One would just be more efficient, then it may not be a problem to load it on every page.
You could consider a PHP stylesheet solution that only executes certain styles if needed. A css.php file may resemble:
<?php
header("content-type: text/css");
/* You can require the blog header to refer to WP variables and make queries */
//require '../../../wp-blog-header.php';
$css = '';
$css .= file_get_contents('style.css');
/* Consider using GET variables or querying a variable in the WP database to determine which stylesheets should be loaded. You could add an option to the backend that allows a stylesheet to be turned on or off. */
if($condition1 == TRUE) $css .= file_get_contents('condition1.css');
if($condition2 == TRUE) $css .= file_get_contents('condition2.css');
?>
Less scripts and less stylesheets means less http requests and a faster load time.