wordpress display image caption if possible, but only on post page - image

I'm trying to make a function I can put into my functions.php that controls the images placed on each post's page. I want any image that has a caption to have that caption display under the image with paragraph tags.

Try this:
function img_caption_only_in_posts( $current_html, $attr, $content ) {
global $post;
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
if ($post->post_type=='page')
$caption="";
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}
add_filter( 'img_caption_shortcode', 'img_caption_only_in_posts',10,3 );
I borrowed the code from this answer and added the code for checking if the post is page or not.

Related

WordPress uploaing blob file

I tried two methods for uploading blob files to WordPress media library.
In first method i tried move blob tmp file to WordPress upload directory with move_uploaded_file(). Then tried upload this temp file with media_handle_sideload() function.
It is blob file that i sent via ajax:
array(
'name' => '2021-03-26_02-58-09',
'type' => 'image/png',
'tmp_name' => 'C:\Users\me\AppData\Local\Temp\php28D8.tmp',
'error' => 0,
'size' => 1880058,
);
Ajax process:
$file = $_FILES['file'];
$upload_dir = wp_upload_dir();
move_uploaded_file( $file['tmp_name'], $upload_dir['path'] . '/' . $file['name'] );
$url = $upload_dir['url'] . '/' . $file['name'];
$tmp = download_url( $url );
$file_array = array(
'name' => basename( $url ),
'tmp_name' => $tmp,
);
$id = media_handle_sideload( $file_array, 0 );
var_dump( $id );
But it returns Sorry, this file type is not permitted for security reasons error.
So i tried this with wp_insert_attachment() function. It is uploading file but without image extension (.jpg, .png) and it is not generating additional image sizes. Here is what i tried:
$file = $_FILES['file'];
$upload_dir = wp_upload_dir();
move_uploaded_file( $file['tmp_name'], $upload_dir['path'] . '/' . $file['name'] );
$id = wp_insert_attachment(
array(
'guid' => $upload_dir['path'] . '/' . $file['name'],
'post_mime_type' => $file['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $file['name'] ),
'post_content' => '',
'post_status' => 'inherit',
),
$upload_dir['path'] . '/' . $file['name']
);
var_dump( $id );

Codeigniter & PHPexcel font size not changing on the first row

When I create a phpexcel document I have set a custom font size.
For some reason the font sizes not changing on the first row.
Question: How to make sure that all rows have the same size including the header row
I have the code in a foreach loop on main controller but miss the first row of cells.
$styleArray = array(
'font' => array(
'size' => 15,
'name' => 'Candara'
));
$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle($objPHPExcel->getActiveSheet()->calculateWorksheetDimension())->applyFromArray($styleArray2);
Controller
<?php
class Events extends MX_Controller {
public function generate_excel() {
$query = $this->db->get('event');
$excelresults = $query->result_array();
require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("");
$objPHPExcel->getProperties()->setLastModifiedBy("");
$objPHPExcel->getProperties()->setSubject("");
$objPHPExcel->getProperties()->setCreator("");
$objPHPExcel->getProperties()->setDescription("");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue("A1", 'Event');
$objPHPExcel->getActiveSheet()->SetCellValue("B1", 'Event Title');
$objPHPExcel->getActiveSheet()->SetCellValue("C1", 'Event Date');
$objPHPExcel->getActiveSheet()->SetCellValue("D1", 'Event Start Time');
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->getActiveSheet()->setPrintGridlines(TRUE);
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$excelrow = 2;
foreach ($excelresults as $excelresult => $excelvalue) {
$styleArray = array(
'font' => array(
'size' => 15,
'name' => 'Candara'
));
$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($styleArray);
if (!empty($excelvalue['fill_color'])) {
// Set the fill style
$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
// Set the colour for the fill
$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->getFill()->getStartColor()->setRGB($excelvalue['fill_color']);
$styleArray1 = array(
'font' => array(
'color' => array('rgb' => $excelvalue['font_color']),
)
);
$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->applyFromArray($styleArray1);
$objPHPExcel->getActiveSheet()->getStyle('A' . $excelrow . ':D' . $excelrow)->getAlignment()->setIndent(2);
}
$objPHPExcel->getActiveSheet()->SetCellValue("A" . $excelrow, $excelvalue['event']);
$objPHPExcel->getActiveSheet()->SetCellValue("B" . $excelrow, $excelvalue['event_title']);
$objPHPExcel->getActiveSheet()->SetCellValue("C" . $excelrow, $excelvalue['event_date']);
$objPHPExcel->getActiveSheet()->SetCellValue("D" . $excelrow, $excelvalue['event_start_time']);
$excelrow++;
}
///exit();
$filename = 'Bowling-Events-For-' . date('Y') . '.xlsx';
$objPHPExcel->getProperties()->setTitle("Riwaka Bowling Club Events");
header("Content-Type: application/vnd.ms-excel; charset=utf-8"); # Important
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".$filename."");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit();
}
}
Are you sure you have attached your correct codes? or are you sure you have tested your attached codes?
I have tested your codes, and its working fine at mine.

Front controller reached 100 router match iterations when run this www.baseurl.com/home/feed url

I am facing magento url error Front controller reached 100 router match iterations when run this www.baseurl.com/home/feed url. please help me resolve this issue.
Here is a way to find out the exact issue
Open the file
app/code/core/Mage/Core/Controller/Varien/Front.php
and find the code segment
while (!$request->isDispatched() && $i++<100) {
foreach ($this->_routers as $router) {
if ($router->match($this->getRequest())) {
break;
}
}
}
replace it with
Mage::log('----Matching routers------------------------------');
Mage::log('Total ' . count($this->_routers) . ': ' . implode(', ', array_keys($this->_routers)));
while (!$request->isDispatched() && $i++<100) {
Mage::log('- Iteration ' . $i);
$requestData = array(
'path_info' => $request->getPathInfo(),
'module' => $request->getModuleName(),
'action' => $request->getActionName(),
'controller' => $request->getControllerName(),
'controller_module' => $request->getControllerModule(),
'route' => $request->getRouteName()
);
$st = '';
foreach ($requestData as $key => $val) {
$st .= "[{$key}={$val}]";
}
Mage::log('Request: ' . $st);
foreach ($this->_routers as $name => $router) {
if ($router->match($this->getRequest())) {
Mage::log('Matched by "' . $name . '" router, class ' . get_class($router));
break;
}
}
}
Reload your site again and you can see the detailed error log at var/log/system.log

randomize default products in opencart

I'm using OpenCart V1.5.3.1 and am trying to randomize or shuffle the products per category on page load.
The other sort options should still work though (per price, rating, alphabetical,..).
Anybody that can give me some pointers?
Many thanks,
Steven
Code I've tried:
In controller/catalog/product/category.php
Just below
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'rating' => $result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])
);
I added:
shuffle($this->data['products']);
In stead of this I also tried this:
Just below:
$results = $this->model_catalog_product->getProducts($data);
I added:
srand((float)microtime() * 1000000);
shuffle($results);
$results = array_slice($results, 0, $data['limit']);
Both these methods unfortunately shuffle also the results when choosing another sorting option (rating, pricing, name). I only want the initial results on page load to be shuffled.
The solution:
Go to catalog/model/catalog/product.php and find the method getProducts($data).
Here change this:
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
to this:
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY RAND()";
}
(hint: only the last $sql .= ... changed)
By this simple change if there is no sorting chosen the products will always be sorted in random order.

Uploading an image with Code Igniter?

I'm trying to set a site up so I can upload images using a web form in Code Igniter(CI). I'm not getting any errors but the file is not being saved either. I wanted to know if those that were successful in uploading images could help explain what might be the issue?
View:
<?php
echo form_open_multipart('admin/galleryUpload') . "\n";
echo "<div class='span-8'><span class='text'>Image:</span>" . form_upload('uploadImg') . "</div>";
foreach ($gallery as $picture)
{
$order[] = $picture->order;
}
$order[] = count($order) + 1;
echo "<div class='span-6 last'><span>Image Order #:</span>" . form_dropdown('order', $order) . "</div><div class='span-14'> </div>";
$conf = array('name' => 'alt_text', 'size' => '75');
echo "<div class='span-14 last'><span>Discription:</span>" . form_input($conf) . "<br /></div>";
echo form_hidden('propertyID', "$propertyID");
echo form_submit('upload', 'Upload');
echo form_close();
?>
Controller:
class Admin extends Controller
{
function galleryUpload()
{
if (! $this->session->userdata('is_admin'))
{
redirect('admin/index');
}
else
{
$this->load->model('admin_model');
$this->admin_model->imgUpload();
}
}
}
Model:
class Admin_model extends Model
{
function imgUpload()
{
$id = $this->input->post('propertyID');
$order = $this->input->post('order');
$alt_text = $this->input->post('alt_text');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
//'upload_path' => '../' . $this->imgPath($id),
'upload_path' => '../img/galleries/temp/',
'max_size' => '5000', // 5MB files max
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->imgPath($id) . '/thumbs',
'maintain_ratio' => TRUE,
'width' => '60'
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
From the user guide:
By default the upload routine expects the file to come from a form
field called userfile, and the form must be a multipart type
So you either have to change the name of the form field to userfile:
form_upload('userfile')
or pass the name of your form field to do_upload:
$this->upload->do_upload('uploadImg');

Resources