Visual Composer not showing specific page styles - php

When I view regular page from visual composer, it works fine like this:
http://vrshealth.com/qc2
Margins, backgrounds, etc are all working.
I needed to make a custom post type "quality-check" and am using archive-quality-check.php to display this and the vc-custom-xxxx styles are not loading for some reason:
http://dev-vrshealth.pantheonsite.io/quality-check/
I did some research and the only thing I could find is that page-specific VC styles don't work with Ajax-loaded pages. But it is not loaded through ajax.
Here is the relevant code from archive-quality-check.php which displays if you haven't already chosen a product lot # to display:
<?php if ($_SERVER['REQUEST_METHOD'] != 'POST'): ?>
<div class="col-xs-12 col-md-12" id="page-content">
<?php
$post_id4098 = get_post(4098);
$content = $post_id4098->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
WPBMap::addAllMappedShortcodes();
echo do_shortcode($content);
?>
</div>
I feel like I must be missing something here, like a function to output metadata or some type of custom css, but I can't find any documentation which explains how.

Had the same problem.
Just insert this before echoing content.
get_post_meta( $id, '_wpb_shortcodes_custom_css', true )
Worked for me on latest WP and VC versions.

The answer that Laurent gave worked great for me! However I would suggest creating a function for it in your functions.php file. Maybe something like this:
function vc_custom_css($id) {
$shortcodes_custom_css = get_post_meta( $id, '_wpb_shortcodes_custom_css', true );
if ( ! empty( $shortcodes_custom_css ) ) {
echo '<style type="text/css">';
echo $shortcodes_custom_css;
echo '</style>';
}
}
Then you can just use vc_custom_css($yourPostID); whenever it is required.

Hi I had the same issues, so I have searched in the pluggin, and finaly this works for me :
$vcM = Vc_Manager::getInstance();
$vc = $vcM->vc();
$vc->addShortcodesCustomCss($pop_up_id);

The issue with vc-custom-xxxx styles described in this topic happening to me quite frequently, with various WordPress Themes.
I have tried an approach with function from the second answer. However there is still the issue, when loading page preview via "Preview changes" button. The style' meta data does not updated unless you manually re-save the page before viewing the preview.
The most correct answer was given by Pierre Mar, but I'd like to put a small note on it. You need to replace $pop_up_id with your current post id.
The final function would look like:
function vc_echo_custom_css($post_id) {
$vcM = Vc_Manager::getInstance();
$vc = $vcM->vc();
$vc->addShortcodesCustomCss($post_id);
}
Include it in your Child's theme functions.php file, and then you can call it from any template like:
vc_echo_custom_css(4098);

Related

WordPress: Add text after (or before) previously posted content based on publish date

I've been searching for a while now with no certain answer. I'm looking to append text to a WordPress site for prior posts that is moving from one domain to a new one and retaining content.
So what I want to do is, add "This article was originally posted at xyz.com." to all posts that were posted before today's date.
Right now this could be done through the database, or a WP functions filter, I'm okay with either option as long as it is long lasting.
Any suggestions on how to go about this would be appreciated?
You can use the_content filter that like this:
add_filter( 'the_content', 'old_wp_content' );
function old_wp_content( $content ) {
if( get_the_date('Y-m-d') < "2017-02-28" ) {
$content = "<p>This article was originally posted at xyz.com.</p>" . $content;
}
return $content;
}
This filter gets fired when you call the_content() of a post. with the_content filter you can adjust the return value of the the_content() function.
I finally got it. That extra "if" before the get_the_date statement and possibly the double quotation marks (swapped for single) wrapping the p tags for the inserted text were the culprits. The following code works:
function old_wp_content( $content ) {
if (get_the_date('Y-m-d') < '2017-02-28' ) {
$content = $content . '<p>This article was originally posted at <a
rel="canonical" href="#">xyz.com</a>.</p>';
}
return $content;
}
add_filter( 'the_content', 'old_wp_content' );
#kevinvhengst, thanks again for your time and patience in helping me figure this out!

Enclosured php element in p-tag is ouside p, how is this possible?

I am using advanced custom fields in wordpress and having this line of code:
<p class="small"><?php the_field('somefield', 'options'); ?></p>
And it is printed like this in the browser:
<p class="small></p>
<p>the content of somefield</p>
Why does this happen and how do I fix it?
it's because you are using paragraph inside and paragraph you can fix it by using the fix below , if you are inserting the <p> inside this field , please use span or something else it will cause this problem if you use <p>:
<?php
$myfield = the_field('somefield', 'options');
echo strip_tags($myfield, '<p></p>');
?>
#Arsh gave a good answer.
Let me explain the inner-workings of it in WordPress Methodology.
Most function which starts with the_{function_name} most of the time has a function get_the_{function_name}.
Since the_{function_name} is a function to output the field and not threat it has data unlike get_the_{function_name}.
Here is a simple example with the_ID()
function the_ID() {
echo get_the_ID();
}
And here is an example with get_the_ID()
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
Understanding those pattern better your understanding of WordPress conventions and its inner workings.
You can always check official documentation to understand WordPress functions.
https://developer.wordpress.org/
or check in with plugin developers docs.

Remove everything from output except between <p> tags

I have a Wordpress site that outputs content from individual blog posts with the_content()
Blog posts all consist of two things, a small gallery and some text:
<div class="gallery"><img></img>Blah Blah</div>
<p>Text</p>
<p>Text</p>
I'd like to split the gallery and the text and output the gallery in a div on the left and the text in a div on the right like this:
<div id="left">GALLERY CONTENT</div>
<div id="right">TEXT CONNTENT</div>
I have tried to do this with strip_tags(the_content(), '<p>') but this does not - it continues to output everything including the gallery.
What is the correct way to do this?
It is not really clear to me what you really want to do , and on to of it , you included some (very little ) source code from output, but to get a real shot at answering you need to include the relative code from the template file.
( And +1 for understanding alone that you should NOT TOUCH YOUR CORE FILES )
Anyhow, I suspect that you only want to disable the auto P generated by wordrpess , so try
remove_filter('the_content', 'wpautop');
(add to functions.php in theme.)
alternatively , you could use
add_filter('use_default_gallery_style', '__return_false');
Which will just "reset" the gallery styling .
or even filter your own gallery styles , which allows you to target them better.
add_filter( 'gallery_style', 'my_own_gallery_style', 99 );
function my_own_gallery_style() {
return "<div class='gallery'>"; // put your own
}
If it does not produce the right output for you, please include more specifics and / or more code .
There are of course more advanced ways to handle this , but Without more info it is difficult to target .
For example you can create your own gallery style by removing the original shortcode function, and then adding your own, but those are a bit more advanced techniques.
// deactivate WordPress function
remove_shortcode('gallery', 'gallery_shortcode');
// activate your own own function
add_shortcode('gallery', 'my_own_gallery_shortcode');
// the own renamed function
function my_own_gallery_shortcode($attr) {
...
}
Now on the other hand , If you want to "catch" some parts of 'the_content' and display it in a loop in a different manner , you can always use a different technique, like described HERE on another answer .
You are using the_content which displays the content instead of returning it.
Change your code to
strip_tags(get_the_content(), '<p>')
I had this exact same problem a while ago. Here's what I did (in single.php, which is where I had the problem):
if ( get_post_format() == 'gallery' ) :
$content = get_the_content();
$gallery_regex = '/\[gallery.*]/s'; //identify the [gallery] tags within the content
//get gallery code
$gallery = preg_match($gallery_regex, $content, $matches);
$gallery = $matches[0];
//remove gallery from content
add_filter('the_content', function($content){
$gallery_regex = '/\[gallery.*]\s*/s';
return preg_replace($gallery_regex, ' ', $content);
});
endif;
Basically, I used regex to remove the gallery tags from the content.
$gallery still contains the shortcode. We can't just randomly display it, or it'll actually show the shortcode. We need to execute it, which will show the output:
if ( get_post_format() == 'gallery' ) {
echo '<div id="left">'. do_shortcode($gallery) .'</div>';
}
Your content no longer contains the gallery, so you can do this:
<div id="right"><?php the_content(); ?></div>

wordpress: loading javascript only where shortcode appears

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.

Wordpress: include content of one page in another

How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.

Categories