Request password on file_download with Advanced Custom Fields - php

So what I want is to request a password when downloading files that are dynamically uploaded via wordPress-plugin Advanced Custom Fields.
I can't get it working, and here's my code:
html:
<div class="documentWrap">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page'); ?>
<?php if(get_field('dokumentgrupp')): ?>
<?php while(has_sub_field('dokumentgrupp')): ?>
<div class="docGroup clearfix">
<h2><?php the_sub_field('gruppnamn'); ?></h2>
<?php if(get_sub_field('filer')): ?>
<?php while(has_sub_field('filer')): ?>
<ul>
<li>
<h3><?php the_sub_field('filnamn'); ?></h3>
ladda ner
<!div id="passWrap">
<div id="passBox">
<?php // if( get_field('password') ): ?>
<form method="post">
<input name="pass" type="password" action="download_pw.php">
<input type="submit" name="submit" value="ladda ner">
</form>
</div>
<?php //endif; ?>
</div>
</li>
</ul>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
download_pw.php:
<?php
$pw = md5($_POST['pass']);
$valid_pw = md5(get_sub_field('password'););
$file_url = get_sub_field("fil_url");
if {$pw != $valid_pw) {
echo 'Tyvärr har du inte tillgång till denna fil';
}else{
header('Location: $file_url');
}
?>

If you are using this HTML on the front end, how about just using the built in password protection on the page/post showing the file?
Wordpress Codex: Using Password Protection
If that's not an option, how about trying something like this: https://wordpress.stackexchange.com/questions/37144/how-to-protect-uploads-if-user-is-not-logged-in

Related

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

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

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.

magento do not display title and logo when manufacturer attribute is set to empty

I am working with the following codes for Magento inside product page view.phtml.
<div class="manufacturer">
<?php if ($_product->getAttributeText('manufacturer') == "Bellazza" || "Della" || "Pet Zone" || "Spyder Auto" ):?>
<div class="dealer">
<p>Authorized Dealer:</p>
</div>
<div class="logoImg">
<?php echo $this->getLayout()->createBlock('manufacturer/manufacturer')->setProduct($_product)->setTemplate('manufacturer/product_manufacturer.phtml')->toHtml();?>
</div>
<?php elseif ?>
<?php endif;?>
</div>
So I create div.manufacturer to display associated brand logo image started by the title "Authorized Dealer". It works. But not all products have a brand so when I set the manufacturer attribute to empty ( refer to the image below ) I don't need the title "Authorized Dealer" anymore. What is the best practice to approach the elseif. Thank you
manufacturer/manufacturer.phtml
<?php
$maufacturers = Mage::getModel('manufacturer/manufacturer')->getCollection()->addFieldToFilter('status',Array('eq'=>1));
$_columnCount=4;
$i=0;
?>
<div class="manufacturer-list">
<?php foreach($maufacturers as $manufacturer): ?>
<?php if(Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($manufacturer->getMenufecturerName())): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul>
<?php endif; ?>
<li>
<a href="<?php echo $this->getBaseUrl()."catalogsearch/advanced/result/?manufacturer[]=".$manufacturer->getMenufecturerName() ?>">
<?php echo $this->getLayout()->createBlock('core/template')->setmanufacturerimage($manufacturer->getFilename())->setlegend($manufacturer->getLegend())->setListPageFlag(1)->setTemplate('manufacturer/manufacturer_resize.phtml')->toHtml(); ?>
</a>
<div class="manufacturer-name">
<?php echo Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($manufacturer->getMenufecturerName()) ?>
</div>
</li>
<?php if ($i%$_columnCount==0 && $i!=count($maufacturers)): ?>
</ul>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
</div>
product_manufacturer
<?php
$product=$this->getProduct();
if(Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($product->getData('manufacturer'))):
$manufacturers=Mage::getModel('manufacturer/manufacturer')->getCollection()->addFieldToFilter('menufecturer_name',$product->getData('manufacturer'));
foreach($manufacturers as $manufacturer){
$status=$manufacturer->getStatus();
if($status==1){
?>
<div class="manufacturer-img-box">
<a href="<?php echo $this->getBaseUrl()."catalogsearch/advanced/result/?manufacturer[]=".$manufacturer->getMenufecturerName() ?>">
<?php echo $this->getLayout()->createBlock('core/template')->setmanufacturerimage($manufacturer->getFilename())->setlegend($manufacturer->getLegend())->setTemplate('manufacturer/manufacturer_resize.phtml')->toHtml(); ?>
</a>
</div>
<?php }
}
endif;
?>
You need refactoring the code
<div class="manufacturer">
<div class="logoImg">
<?php echo $this->getLayout()->createBlock('manufacturer/manufacturer')->setProduct($_product)->setTemplate('manufacturer/product_manufacturer.phtml')->toHtml();?>
</div>
</div>
and check variable
<?php if(!is_null($manufacturer->getMenufecturerName())): ?>
<div class="manufacturer-name">
<?php
echo Mage::getModel('eav/entity_attribute_source_table')
->setAttribute(Mage::getModel('eav/entity_attribute')
->load(Mage::getModel('eav/entity_attribute')
->getIdByCode('catalog_product',"manufacturer")))
->getOptionText($manufacturer->getMenufecturerName())
?>
</div>
<?php endif; ?>
And you need refactoring your code. Because I loock much excess code in your template. You need get data in you block and in phtml you need render this data by API blocks. getImageSrc(); getManufactures() and more over methods for get processed information.

page.php overrides custom page template?

I can't get rid of a right sidebar in a custom page template. Something seems to be passing from page.php to the custom page template and I don't know how to prevent this. The custom page template code is simple:
<?php
/* Template Name: sample template */
get_header(); ?>
<div id="body">
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
Below is my page.php code, inherited from a previous developer:
<?php get_header(); ?>
<div id="body">
<div class="align left">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<br />
<?php the_title(); ?>
<br />
<?php // include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' =>'Pages: ', 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php // comments_template(); ?>
<?php endwhile; endif; ?>
</div>
<div class="align right">
<?php
global $post;
$showImage = true;
$url = $url = WP_CONTENT_URL . '/uploads/';
$file = get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attached_file', true);
if ( $file == ''){
$showImage = false;
}
$url .= $file;
if ( $showImage ){
?>
<img src="<?php echo $url; ?>" alt="" />
<?php
}
?>
</div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>
I don't want to edit page.php to get a full-width custom page. How can I achieve this in the custom page template?
You can use get_sidebar(); function.
you can know more about this function here http://codex.wordpress.org/Function_Reference/get_sidebar

Wordpress Comments Section Not Showing

I'm at a loss to for a solution to my comments section not showing on my single post page. I've noticed that this page is not displaying any of the code below the post and tags sections. Would someone be willing to look at the code in the admin for me? I imagine it could be something simple to identify, as the site is organized nicely.
This is the only way I think the problem could be solved.
UPDATE UPDATE UPDATE. Here's Single.php
<?php get_header(); ?>
<?php include ('page_header.php'); ?>
<?php include ('page_navigation.php'); ?>
<div id="page-content" class="clearfix">
<div id="content-container" class="clearfix">
<div id="main-content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ( get_option('minimax_post_layout') == 'head-three' ) { ?>
<h2><?php the_title(); ?><em>Posted on <?php the_time('D, M d, Y') ?></em></h2>
<?php if ( has_post_thumbnail() ) { ?>
<div class="post-item-thumb-<?php echo get_option('minimax_post_layout'); ?>">
<?php the_post_thumbnail('thumb_post_3'); ?>
</div>
<?php } ?>
<?php } else { ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="post-item-thumb-<?php echo get_option('minimax_post_layout'); ?>">
<?php the_post_thumbnail('thumb_post_2'); ?>
</div>
<?php } ?>
<h2><?php the_title(); ?><em>Posted on <?php the_time('D, M d, Y') ?></em></h2>
<?php } ?>
<?php the_content(); ?>
<?php edit_post_link('Edit', '<br /><p>', '</p>'); ?>
<?php include ('post_related.php'); ?>
<div id="post-indexing"> <?php the_tags('<strong>Tagged as</strong> ',', ','+'); ?><br /><strong>Categorized as</strong> <?php the_category(', ','single'); ?></div>
<?php include ('post_author.php'); ?>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no pages matched your criteria.'); ?></p>
<?php endif; ?>
</div><!-- end main-content -->
<?php include ('sidebar_post.php'); ?>
</div><!-- end content-container -->
<?php include ('footer_columns_posts.php'); ?>
</div><!-- end page-content -->
<?php get_footer(); ?>
Also, here's the comments.php page:
<div id="comments">
<?php // Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
}
/* This variable is for alternating comment background */
$oddcomment = 'class="alt" ';
?>
<!-- You can start editing here. -->
<?php if ($comments) : ?>
<h2>Comments <span>(<?php comments_number('No Responses', 'One Response', '% Responses' );?
>)</span></h2>
<ul class="commentlist">
<?php wp_list_comments('avatar_size=60&type=comment'); ?>
</ul>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<?php endif; ?>
<?php endif; ?>
<?php if ('open' == $post->comment_status) : ?>
<div id="respond">
<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>
<div class="cancel-comment-reply">
<?php cancel_comment_reply_link(); ?>
</div>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo
urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>
<div id="post-comment" class="clearfix">
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post"
id="commentform">
<fieldset>
<?php if ( $user_ID ) : ?>
<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo
$user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?
action=logout" title="Log out of this account">Log out »</a></p>
<?php else : ?>
<label for="author"><?php if ($req) echo "* "; ?>Your name:</label>
<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22"
tabindex="1" />
<label for="email"><?php if ($req) echo "* "; ?>Mail (will not be published):</label>
<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>"
size="22" tabindex="2" />
<label for="url">Website</label>
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22"
tabindex="3" />
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?
></code></small></p>-->
<label for="comment" class="comment">Your comment:</label>
<textarea name="comment" id="comment" class="comment" cols="61%" rows="10"
tabindex="4"></textarea>
<input name="submit" type="submit" class="submit" alt="Submit Comment" id="submit" tabindex="5"
value="Submit Comment" />
<input type="hidden" class="hide" name="comment_post_ID" value="<?php echo $id; ?>" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>
</fieldset>
</form>
</div><!-- end post-comment -->
</div>
<?php endif; // If registration required and not logged in ?>
<?php endif; // if you delete this the sky will fall on your head ?>
</div>
Here is post_author.php
<?php if ( get_option('minimax_author') == 'yes' ) { ?>
<div id="author-desc" class="clearfix">
<?php the_author_image(); ?>
<h4>About the Author</h4>
<?php the_author_description(); ?>
</div>
<?php } ?>
There isn't any function like the_author_image() in Wordpress. So, you have to remove
<?php the_author_image(); ?>
line from your post_author.php. If you want to show author image , Wordpress getting avatars from Gravatar. You can use get_avatar() to display author's Gravatar.
echo get_avatar(get_the_author_id());
In shortly, use this :
<?php echo get_avatar(get_the_author_id()); ?>
instead of this :
<?php the_author_image(); ?>

Categories