Extract html code from a page with PHP to extrapolate some images - php

I've to create a PDF document from a product details page of my WP site and I've extract all html code from the page to extrapolate img and table tag.
I installed a plugin to create a PDF document from a page but imagese and tables are not inserted: only the text is inserted into the document.
The plugin is woocommercePrintProduct and I've edited its code to insert images into PDF document also. The function that should be modified is the following:
div class="descrizione" style="margin:0 30px;">
<div class="colonnaSx" style="float:left;width:350px;margin-right:30px">
<div class="pacchettino">
<?php if( get_field('descrizione_prod') ): ?>
<h3>DESCRIPTION</h3>
<p><?php the_field('descrizione_prod'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('forma') ): ?>
<h3>SHAPE</h3>
<p><?php the_field('forma'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('posa') ): ?>
<h3>INSTALLATION</h3>
<p><?php the_field('posa'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('conduttore') ): ?>
<h3>CONDUCTOR</h3>
<p><?php the_field('conduttore'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('isolante') ): ?>
<h3>INSULATION</h3>
<p><?php the_field('isolante'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('guaina') ): ?>
<h3>SHEAT</h3>
<p><?php the_field('guaina'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('sforzo_massimo_di_tiro') ): ?>
<h3>MAX MECHANICAL STRESS</h3>
<p><?php the_field('sforzo_massimo_di_tiro'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('guainetta') ): ?>
<h3>INTERNAL SHEATH</h3>
<p><?php the_field('guainetta'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="colonnaDx" style="float:right;width:350px;">
<div class="pacchettino">
<?php if( get_field('marchiatura') ): ?>
<h3>MARCHIATURA</h3>
<p><?php the_field('marchiatura'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('colore') ): ?>
<h3>COLORE</h3>
<p><?php the_field('colore'); ?></p>
<?php endif; ?>
</div>
<div class="pacchettino">
<?php if( get_field('tensione_di_esercizio') ): ?>
<h3>TENSIONE DI ESERCIZIO</h3>
<p><?php the_field('tensione_di_esercizio'); ?></p>
<?php endif; ?>
</div>
<?php /*
<table class="description">
<?php
$description = wpautop($this->data->description);
$description = preg_replace("/\[[^\]]+\]/", '', $description);
$description = explode('<p>', $description);
foreach ($description as $value) {
?>
<tr>
<td style="padding-bottom: 20px;">
<?php echo $value ?>
</td>
</tr>
<?php
}
?>
</table>
*/
?>
<div class="description" style="margin:0 30px;">
<?php
$description = wpautop($this->data->description);
$description = preg_replace("/\[[^\]]+\]/", '', $description);
$description = explode('<p>', $description);
foreach ($description as $value) {
?>
<div class="pacchettino" style="padding-bottom: 20px;margin:0 30px;">
<?php
echo $value
?>
</div>
<?php
}
?>
</div>
<?php
return ob_get_clean();
}
I thought to extract html code from the details product page and then, extrapolate img tag and intert them into the document.
Can you help me, please?

There are much, much easier solutions for this.
I recommend WKHTMLtoPDF, it is open source, no browser, no app, just command line, extremely simple.
http://wkhtmltopdf.org/downloads.html
How to use:
After installing from the link above
Open CMD prompt
Type in
wkhtmltopdf {website} {filename to save as}
Example:
wkhtmltopdf http://google.com google.pdf
User Manual:
http://wkhtmltopdf.org/usage/wkhtmltopdf.txt
There are numerous other programs and even online tools that can do exactly what you ask at no charge, many are even open source like the one above. There is no point shaving a square peg to fit in a round hole when you can get cylinders for free.

Related

How to Split list of content after a certain amount of rendered elements

I am trying to get a block to split content after so many entry's have been selected by the user in the backend of my website.
This is what is is supposed to look like
However at the moment I cannot get it to break off a second line. here is the code below to show how I am rendering my block along with an image of what is being rendered
function roomFacilitiesLists(){
$roomFacilitiesList1 = get_field ("facilities_list_1") ;
$roomFacilitiesList2 = get_field ("facilities_list_2") ;
$roomFacilitiesList3 = get_field ("facilities_list_3") ;
$roomFacilitiesLeftSideImage = get_field ("room_facilities_left_side_image") ;
$roomFacilitysBackgroundImage = get_field ("room_facilitys_background_image") ;
?>
<div class="left-side-image-block-parent">
<div class="left-sided-image">
<img src="<?php echo $roomFacilitiesLeftSideImage['url']; ?>" ; />
</div>
</div>
<div class="right-side-room-facilitys">
<div class="room-facilities-lists" style="background-image: url(<?php echo $roomFacilitysBackgroundImage['url']; ?>)"> ;
</div>
<div class="main-lists-container">
<div class="column-1-list">
<?php
if( $roomFacilitiesList1 ): ?>
<ul>
<?php foreach( $roomFacilitiesList1 as $roomFacilitiesList1 ): ?>
<div><?php echo $roomFacilitiesList1; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-3-list columns-2">
<?php
if( $roomFacilitiesList3 ): ?>
<ul>
<?php foreach( $roomFacilitiesList3 as $roomFacilitiesList3 ): ?>
<div><?php echo $roomFacilitiesList3; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-2-list">
<?php
if( $roomFacilitiesList2 ): ?>
<ul>
<?php foreach( $roomFacilitiesList2 as $roomFacilitiesList2 ): ?>
<div><?php echo $roomFacilitiesList2; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
Now this makes up 3 columns of data however the other 2 are not the issue here is "columns-3" which is the data output causing the issues.
I have tried using https://www.php.net/manual/en/function.str-split.php as well as https://www.php.net/manual/en/function.explode but with no prevail any help here would be great.

Get parameter of shortcode in Insert PHP Code Snippet plugin using ACF repeater

Everybody hello! I'm sorry for asking such kind easy question, I'm pretty new at this. I have problem with transfering parameter value and echoing it to itself.
PHP code snippet on page:
[xyz-ips snippet="Generating-content" paramSet="1"]
Inside 'generating content':
<?php if( have_rows('information') ): ?>
<?php while( have_rows('information') ): the_row(); ?>
<?php $sub_value = get_sub_field('number');
if($sub_value == 1): ?>
<div class="d-block d-md-flex d-lg-flex">
<img src="<?php the_sub_field('image'); ?>">
<div>
<?php if( have_rows('repeatable_content') ): ?>
<?php while( have_rows('repeatable_content') ): the_row(); ?>
<p>
<?php the_sub_field('text'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Right now I need to change condition if($sub_value == 1) to if($sub_value == "PARAMETER VALUE")
Please help, guys!
I think that is what you are looking for.Not tested but it should work.
<?php
$paramSet = (!isset($atts['paramSet'])) ? $atts['paramSet'] : 'default';
if (have_rows('information')) : ?>
<?php while (have_rows('information')) : the_row(); ?>
<?php $sub_value = get_sub_field('number');
if ($sub_value == $paramSet) : ?>
<div class="d-block d-md-flex d-lg-flex">
<img src="<?php the_sub_field('image'); ?>">
<div>
<?php if (have_rows('repeatable_content')) : ?>
<?php while (have_rows('repeatable_content')) : the_row(); ?>
<p>
<?php the_sub_field('text'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
Read More - https://developer.wordpress.org/reference/functions/shortcode_atts/
Thank you guys for helping! The main issue of this problem is that you have to buy premium version of this plugin, your version is working in another plugins :)
Working plugin's name: Shortcodes Blocks Creator Ultimate

Drop Cap and P tag not respected on description

I have this code in my event_single.php in tribe_events folder under theme. I've been trying to do the drop cap but it seems that this is not working and even the p tag is not being respected.
<div id="post-<?php the_ID(); ?>" <?php post_class('event__main'); ?>>
<?php while (have_posts()): the_post(); ?>
<div class="event__description page__content">
<h3 class="event__description__title"><?php _e('Description') ?></h3>
<?php do_action('tribe_events_single_event_before_the_content') ?>
<?php
$content = apply_filters('the_content', get_the_content());
echo preg_replace('/<p>(.)/', '<p><span class="drop-cap">$1</span>', $content, 1);
?>
<?php do_action('tribe_events_single_event_after_the_content') ?>
</div>
<div class="event__details">
<?php do_action('tribe_events_single_event_before_the_meta') ?>
<?php tribe_get_template_part('modules/meta'); ?>
<?php do_action('tribe_events_single_event_after_the_meta') ?>
</div>
<?php endwhile; ?>
</div>

wp_reset_postdata killing variable data

I've created an archive page for a custom post type in Wordpress. I want to output data if certain fields match.
Within the first if statement, I want to check what product the user has signed up to (held within $product and match it against the current items custom field (held within $title).
All it's doing is seeing what the user signed up to then outputting the content (which is all the data below <?php if($match == "yes"){ ?>.
The problem I have is wp_reset_postdata() is killing the data held within $match. Is there a way around this? If I don't include the setting up of post data and don't include the reset then the rest of the page doesn't show the correct info.
I'm using Advanced Custom Fields relationship field (http://www.advancedcustomfields.com/resources/field-types/relationship/).
Any help would be much appreciated.
User signed up as: <?php $current_user = wp_get_current_user();
$board = $current_user->work;
$product = $current_user->product;
echo $product; ?>
<br />
<?php while (have_posts()) : the_post(); ?>
<?php $products = get_field('product');
if( $products ):
foreach( $products as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$title = $post->post_title;
echo $title;
if($product == $title) {
$match = "yes";
}
endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif; ?>
<?php if($match == "yes"){ ?>
<div id="module-area" style="margin-top:0px!IMPORTANT;">
<div id="modules-top"></div>
<div id="modules-repeat" style="position:relative;padding-left:10px;padding-right:10px;width:625px!IMPORTANT;">
<div class="topic">
<p><?php the_title(); ?></p>
</div>
<div class="description">
<p><?php the_field('description') ?></p>
</div>
<div class="date">
<p style="text-align:center!IMPORTANT;"><?php the_modified_time('d.m.y'); ?></p>
</div>
<div class="action">
<a class="train-button" href="<?php echo the_permalink(); ?>"></a>
</div>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
<?php } ?>
<?php endwhile; ?>
User signed up as: <?php $current_user = wp_get_current_user();
$board = $current_user->work;
$product = $current_user->product;
?>
<?php $products = get_field('product');
if( $products ):
foreach( $products as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$title = $post->post_title;
if($product == $title) {
wp_reset_postdata(); ?>
<div id="module-area" style="margin-top:0px!IMPORTANT;">
<div id="modules-top"></div>
<div id="modules-repeat" style="position:relative;padding-left:10px;padding-right:10px;width:625px!IMPORTANT;">
<div class="topic">
<p><?php the_title(); ?></p>
</div>
<div class="description">
<p><?php the_field('description') ?></p>
</div>
<div class="date">
<p style="text-align:center!IMPORTANT;"><?php the_modified_time('d.m.y'); ?></p>
</div>
<div class="action">
<a class="train-button" href="<?php echo the_permalink(); ?>"></a>
</div>
<div class="clear"></div>
</div>
<div style="margin-bottom:5px;" id="modules-bottom"></div>
</div>
<?php
}
endforeach;
endif; ?>
wp_reset_postdata();

Controlling Magenot sort collectio by attribute

I am using the FishPig module to display WordPress on my Magento site. I have selected some associated blog posts from a CMS Page and they display as I need to.
However I am using the postion boxes but this doesn't seem to have any effect.
I have tried adding ->addAttributeToSort('position') to the getPostCollection() but this causes the page to break.
Full code as it is now.
<?php if (($posts = $this->getPostCollection()) !== false): ?>
<?php if (count($posts) > 0): ?>
<div class="box-collateral box-description" style="padding:5px 20px;">
<!--<?php if ($title = $this->getTitle()): ?>
<h2 style="margin-bottom:15px;">Blog <span style="color: #68d088;">Posts</span></h2>
<?php endif; ?>-->
<div class="std">
<ul>
<?php foreach($posts as $post): ?>
<li class="related-blogs">
<h2><?php echo $this->escapeHtml($post->getPostTitle()) ?></h2>
<div class="content">
<?php
$pos = strpos($post['post_content'], ' ', 300);
echo substr($post['post_content'],0,$pos );
?>
</div>
<a style="color:#68d088; font-weight:bold;" href="<?php echo $post->getPermalink() ?>">Read More...</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
<?php endif; ?>

Categories